These are the type of operators that act upon just a single operand for producing a new value. All the unary operators have equal precedence, and their associativity is from right to left. When we combine the unary operator with an operand, we get the unary expression.
In this article, we will dig deeper into the Unary Operator in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.
Table of Contents
- Types Of Unary Operator In C
- Functions Of Unary Operator In C
- Practice Problems On Unary Operator In C
- Faqs
Types of Unary Operator in C
There are following types of unary operators found in the C language:
- unary minus (-)
- unary plus (+)
- decrement (- -)
- increment (++)
- NOT (!)
- sizeof ()
- Address of operator (&)
Functions of Unary Operator in C
Here is how we use all the unary operators:
Unary Minus
This operator changes the sign of any given argument. It means that a positive number will become negative, and a negative number will become positive.
But the unary minus is different from that of the subtractor operator. It is because we require two operands for subtraction.
int p = 20;
int q = -p; // q = -20
Unary Plus
This operator changes the sign of any negative argument. It means that a negative number will become positive, and a positive number will also become positive.
But the unary minus is different from that of the subtractor operator. It is because we require two operands for subtraction.
int p = -20;
int q = +p; // q = 20
NOT (!)
We use this operator for reversing the logical state of the available operand. It means that the logical NOT operator will make an available condition to be false if it is already true.
In simple words,
- If p is true, then !p will be false.
- If p is false, then !p will be true.
Increment
We use this operator to increment the overall value of any given variable by a value of 1. We can perform increment using two major ways:
- Prefix increment
- Postfix Increment
Prefix Increment
The operator in this method precedes the given operand (Example, ++p). Thus, the value of the operand gets altered before we finally use it.
For instance,
int p = 1;
int q = ++p; // q = 2
Postfix Increment
The operator in this method follows the given operand (Example, p++). Thus, the value of the available operand gets altered after we use it.
For instance,
int p = 1;
int q = p++; // q = 1
int r = p; // r = 2
Decrement
We use this operator to decrement the overall value of any given variable by a value of 1. We can perform decrement using two major ways:
Prefix Decrement
The operator in this method precedes the given operand (Example, –p). Thus, the value of the operand gets altered before we finally use it.
For instance,
int p = 1;
int q = –p; // q = 0
Postfix Decrement
The operator in this method follows the given operand (Example, p–). Thus, the value of the available operand gets altered after we use it.
For instance,
int p = 1;
int q = p–; // q = 1
int r = p; // r = 0
Address of Operator (&)
This type of operator provides the user with the address of any variable. The address of operator is used to return the address (memory address) of any variable. The addresses that are returned using this operator are called pointers. It is because they point towards the variable present in the memory.
For instance,
& gives an address on variable n
int a;
int *ptr;
ptr = &a; // address of a is copied to the location ptr.
Sizeof ()
The function of the size of operator is to return the original size of the available operand in terms of bytes. This type of operator always precedes the given operand. Here, the operand can either be an expression or it can also be a cast.
For instance,
#include <iostream>
using namespace std;
int main()
{
float n = 0;
cout << “size of n: ” << sizeof(n);
return 1;
}
The output obtained here would be –
size of n: 4
Practice Problems on Unary Operator in C
1. Which of these is a unary operator in the C language?
1. !
2. sizeof
3. ~
4. &&
A. 1, 2
B. 1, 3
C. 2, 4
D. 1, 2, 3
Answer – D) 1, 2, 3
2. What is the function of the ++ operator in the C programming language?
A. It is a unary operator
B. The operand may come after or before an operator
C. It increases the value of the available variable
D. All of the above
Answer – D) All of the above
FAQs
What is the difference between the unary plus and the unary minus?
The unary minus operator changes the sign of any negative argument. It means that a negative number will become positive, and a positive number will also become positive. The unary plus operator changes the sign of any given argument. It means that a positive number will become negative, and a negative number will become positive.
What is the difference between prefix and postfix increment/ decrement?
The operator in the prefix increment/ decrement method precedes the given operand (Example, ++p and –p). Thus, the value of the operand gets altered before we finally use it. The operator in the postfix increment/ decrement method follows the given operand (Example, p++, p–). Thus, the value of the available operand gets altered after we use it.
Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Syllabus for CSE (Computer Science Engineering), GATE CSE Notes, GATE CSE Question Paper, and more.
Also Explore,
Comments