Professional Documents
Culture Documents
Unary Operators in C
Unary Operators in C
Unary Operators in C
While most of the operators in C are binary in nature, there are a few unary
operators as well. An operator is said to be unary if it takes just a single operand,
unlike a binary operator which needs two operands.
Some operators in C are binary as well as unary in their usage. Examples of unary
operators in C include ++, --, !, etc.
The statement a++ is equivalent to writing "a = a + 1." The "++" operator can
appear before or after the operand and it will have the same effect. Hence, a++ is
equivalent to ++a.
However, when the increment operator appears along with other operators in an
expression, its effect is not the same. The precedence of "prefix ++" is more than
"postfix ++". Hence, "b = a++;" is not the same as "b = ++a;"
In the former case, "a" is assigned to "b" before the incrementation; while in the
latter case, the incrementation is performed before the assignment.
The "--" operator can appear before or after the operand and in either case, it will
have the same effect. Hence, "a--" is equivalent to "--a".
However, when the decrement operator appears along with other operators in an
expression, its effect is not the same. The precedence of "prefix --" is more than
"postfix --". Hence, "b = a--" is not the same as "b = --a".
In the former case, "a" is assigned to "b" before the decrementation; while in the
latter case, the decrementation is performed before the assignment.
https://www.tutorialspoint.com/cprogramming/c_unary_operators.htm 1/8
6/16/24, 11:51 AM Unary Operators in C
The "+" operator is present implicitly whenever a positive value is assigned to any
numeric variable. The statement "int x = 5;" is same as "int x = +5;". The same
logic applies to float and char variable too.
Example
#include <stdio.h>
int main(){
char x = 'A';
char y = +x;
float a = 1.55;
float b = +a;
return 0;
}
Output
When you run this code, it will produce the following output −
x: A y: A
a: 1.550000 y: 1.550000
https://www.tutorialspoint.com/cprogramming/c_unary_operators.htm 2/8
6/16/24, 11:51 AM Unary Operators in C
negation operator in C.
Example
In this code, the unary negation operator returns the negative value of "x" and
assigns the same to another variable "y".
#include <stdio.h>
int main(){
int x = 5;
int y = -x;
return 0;
}
Output
x: 5 y: -5
Example
The & operator returns the memory address of its variable operand. Take a look at
the following example −
#include <stdio.h>
int main(){
char x = 'A';
printf ("Address of x: %d\n", &x);
https://www.tutorialspoint.com/cprogramming/c_unary_operators.htm 3/8
6/16/24, 11:51 AM Unary Operators in C
return 0;
}
Output
Address of x: 6422047
char x = 'A';
printf ("Address of x: %p\n", &x);
Address of x: 000000000061FE1F
int x = 10;
int *y = &x;
When you want to store the memory address of a variable, the variable should be
declared with an asterisk (*) prefixed to it.
int x = 10;
int *y = &x;
https://www.tutorialspoint.com/cprogramming/c_unary_operators.htm 4/8
6/16/24, 11:51 AM Unary Operators in C
Here the variable "y" stores the address of "x", hence "y" acts as a pointer to "x". To
access the value of "x" with the help of its pointer, use the dereference operator (*).
Example 1
#include <stdio.h>
int main(){
int x = 10;
int *y = &x;
return 0;
}
Output
x: 10 Address of x: 6422036
Value at x with Dereference: 10
Example 2
You can also assign a value to the original variable with the help of the dereference
pointer −
#include <stdio.h>
int main(){
int x = 10;
int *y = &x;
https://www.tutorialspoint.com/cprogramming/c_unary_operators.htm 5/8
6/16/24, 11:51 AM Unary Operators in C
*y = 20;
return 0;
}
Output
x: 10 Address of x: 6422036
x: 20 with dereference: 20
Example 1
#include <stdio.h>
int main(){
int a = 0;
int b = 20;
return 0;
}
Output
https://www.tutorialspoint.com/cprogramming/c_unary_operators.htm 6/8
6/16/24, 11:51 AM Unary Operators in C
Example 2
#include <stdio.h>
int main(){
int i = 0;
return 0;
}
Output
In this code, the while loop continues to iterate till the expression "!(i > 5)"
becomes False, which will be when the value of "i" becomes more than 5.
i=0
i=1
i=2
i=3
i=4
i=5
a ~a
https://www.tutorialspoint.com/cprogramming/c_unary_operators.htm 7/8
6/16/24, 11:51 AM Unary Operators in C
0 1
1 0
Assuming that the int variable "a" has the value 60 (equivalent to 0011 1100 in
binary), the "~a" operation results in -61 in 2’s complement form, as per the bitwise
right-shift of its corresponding bits.
Example
#include <stdio.h>
int main(){
return 0;
}
Output
When you run this code, it will produce the following output −
Value of c is -61
https://www.tutorialspoint.com/cprogramming/c_unary_operators.htm 8/8