The unary operator minus minus decrease the value by 1 for the specified number variable. It can be used either pre-decrement or post-decrement. The post-decrement and pre-decrement makes difference when it used in expression or statement.
The pre-decrement is used in the expression, it decrease the variable value by 1 first and than used its value in the expression or statement. The post-decrement used value first in the expression and than decrease the value.
1 <variable>--
2 --<variable>
Unary operator minus minus
1 int a = 10;
2 a--;
3 --a;
4 a-- * 10;
5 --a * 10;
Unary minus with post decrement
Unary minus with post decrement
1 #include <stdio.h>
2
3 int main() {
4
5 int a = 15, b = 10;
6
7
8 printf("\nValue of variable A is : %d", a--);
9 printf("\nCurrent value after post decrement : %d", a);
10
11 printf("\nUnary minus with expression : %d", b-- * 2);
12 printf("\nCurrent value of b is : %d", b);
13
14 return 0;
15 }
In the above example, an integer variable is declare and initialized with value. A variable a value is printed that specified with unary minus which will prints the current value and decrement by 1. Hence, in the next print statement, it prints value of variable a which is decreased by 1, ie 14.
In the next example, a variable b is specified with unary minus that multiply with 2, it multiplies the current value (10) with 2 and decreased by 1, hence next print statement prints variable b with value 9.
1 Value of variable A is : 15
2 Current value after post decrement : 14
3 Unary minus with expression : 20
4 Current value of b is : 9
Multiple unary minus in same expression
Multiple unary minus in same expression
1 #include <stdio.h>
2
3 int main() {
4
5 int a = 15, res;
6
7
9 res = a-- + a --;
10 printf("\nExpression result : %d", res);
11 printf("\nCurrent value of b is : %d", a);
12
13 return 0;
14 }
In the above example, a unary minus is used twice in the same expression, a next unary operator uses an updated value of same variable. Hence, first a-- uses 15 and next a-- uses 14. So, resulting addition value will be 29 (15 + 14). A result will be printed and next print statement prints current value after decreasing twice (13).
1 Expression result : 29
2 Current value of b is : 13
Unary minus with pre decrement
Unary minus with pre decrement
1 #include <stdio.h>
2
3 int main() {
4
5 int a = 15, b = 10;
6
7
8 printf("\nValue of variable A is : %d", --a);
9 printf("\nCurrent value after post decrement : %d", a);
10
11 printf("\nUnary minus with expression : %d", --b * 2);
12 printf("\nCurrent value of b is : %d", b);
13
14 return 0;
15 }
In the above example, a variables are declare and initialized with integer values. A value of variable a is printed using unary minus pre-decrement which will decrease the variable value a first. Hence both prints statements prints the same value.
In the second example, a variable b printed with unary minus and multiply with 2. A variable value will be decrease first and than multiply with 2. Hence both prints statement uses a same value of variable b.
1 Value of variable A is : 14
2 Current value after post decrement : 14
3 Unary minus with expression : 18
4 Current value of b is : 9
Note : If unary minus with pre-decrement used multiple time, it decreases value of variable first and than uses value of variable.
Related options for your search