If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In C programming, unary arithmetic operators are used to perform mathematical operations on a single operand (variable or constant). Here are the most common unary arithmetic operators used in C:
++
: increment operator. It increases the value of the operand by 1.Copy code
int x = 5;
x++;
This will increment x by 1, resulting in x = 6
.
--
: decrement operator. It decreases the value of the operand by 1.Copy code
int y = 7;
y--;
This will decrement y by 1, resulting in y = 6
.
+
: unary plus operator. It does not change the value of the operand.Copy code
int z = +5;
This will not change the value of z, resulting in z = 5
.
-
: unary minus operator. It changes the sign of the operand (positive to negative or negative to positive).Copy code
int a = -5;
This will change the sign of a to negative, resulting in a = -5
.
It's important to note that unary operators have a higher precedence than binary operators in C. Also, it's important to be aware of the position of the operator, whether it's prefix or postfix, because it can change the value of the operand. In the examples above, I have used postfix increment and decrement (x++ and y--), but you can also use prefix increment and decrement (++x and --y) which will change the value of x or y before the expression is evaluated.
Comments: 0