Home »
C programming language
Unary Operators in C/C++ programming language with Examples
In this tutorial, we will learn about the unary operators and their usages with examples in C/C++ programming languages.
What are Unary Operators?
The operators which operates on single operand (i.e. to perform an operation through these operators, we need only one operand).
List of Unary Operators in C programming language
Following are the unary operators in C and C++ programming language
SrNo | Operators |
Symbols |
1 | Unary plus |
+ |
2 | Unary minus |
- |
3 | Increment operator |
++ |
4 | Decrement operato |
-- |
5 | Address of Operator |
& |
6 | Size of Operator |
sizeof() |
7 | Dereferencing Operator |
* |
8 | Logical NOT |
! |
9 | Bitwise NOT/ Bitwise Negation/ One's Compliment |
~ |
1) Unary plus (+) Operator
This operator does not make any effect on the operand value, it just returns operands value.
Consider the given example:
#include <stdio.h>
int main()
{
int x= +4;
printf("x= %d\n",x);
return 0;
}
Output
x= 4
Here, we assigned +4 to the variable x and the result is 4.
2) Unary minus (-) Operator
This operator makes the value negative. It makes positive value to negative and negative value to positive.
Consider the given example:
#include <stdio.h>
int main()
{
int x=10;
int y=-20;
printf("value of -x: %d\n",-x);
printf("value of -y: %d\n",-y);
return 0;
}
Output
value of -x: -10
value of -y: 20
Here, we assigned 10 to variable x and -20 to variable y, when we print the value of both variables using Unary minus operator, the result is -x= 10 and -y= 20.
3) Increment (++) Operator
This operator increases the value by 1; there are two varieties of increment operator 1) pre-increment operator and 2) post-increment operator.
For example: if we want to increase the value of variable x, the pre-increment operation will be written like ++x and post-increment operation will be written like x++.
4) Decrement (--) Operator
This operator decreases the value by 1; there are two varieties of decrement operator 1) pre-decrement operator and 2) post-decrement operator.
For example: if we want to decrease the value of variable x, the pre-decrement operation will be written like --x and post-decrement operation will be written like x--.
5) Address of (&) Operator in C
This operator returns address of any variable.
Consider the given example
#include <stdio.h>
int main()
{
int x=10;
printf("Value of x: %d\n",x);
printf("Address of x: %X\n",&x);
return 0;
}
Output
Value of x: 10
Address of x: F0C10E3C
6) sizeof() Operator
This is a function like operator that returns the occupied size of any variable, object, constant etc, even this operator returns the size of any data type, literal etc.
#include <stdio.h>
int main()
{
int x=10;
printf("size of x : %d\n",sizeof(x));
printf("size of 10 : %d\n",sizeof(10));
printf("size of int : %d\n",sizeof(int));
printf("size of char : %d\n",sizeof(char));
printf("size of float: %d\n",sizeof(float));
return 0;
}
Output
size of x : 4
size of 10 : 4
size of int : 4
size of char : 1
size of float: 4
7) Dereferencing (*) Operator
This operator represents by character asterisk (*), it is used to deference the value of any pointer variable.
Let suppose, there is a variable pointer variable ptr which has been initialised with the address of variable num and num holds value 10.
Then, to access the value of num using ptr, we use dereferencing operator (*).
Consider the given example
#include <stdio.h>
int main()
{
int num=10;
int *ptr=#
printf("Value of num is: %d\n",*ptr);
return 0;
}
Output
Value of num is: 10
8) Logical NOT (!) operator
This operator inverse the result of any expression, if expression's result is non zero it returns zero (0) and if expression's result is zero its returns 1.
Consider the given example
#include <stdio.h>
int main()
{
int x = !(0);
int y = !(1);
int z = !(10);
printf("x= %d, y= %d, z= %d\n",x,y,z);
return 0;
}
Output
x= 1, y= 0, z= 0
9) Bitwise NOT (~) Operator
This operators also known as Bitwise negation and one’s compliment operator in C language, it is a Unary operator in C and C++, it converts (inverse) individual bits from 0 to 1 and 1 to 0.
For example: there is a variable x with value 0xAA (in binary 1010 1010), ~x will be 0x55 (in binary 0101 0101).
Consider the given example
#include <stdio.h>
int main()
{
unsigned char a=0xAA;
printf("Before negation value of a: %02X\n",a);
a=~a;
printf("After negation value of a: %02X\n",a);
return 0;
}
Output
Before negation value of a: AA
After negation value of a: 55