++ and - - are called as unary operators.
The increment (++) operator increments the variable by one and decrement(- -)
operator decrements variable by one.
If increment/decrement operator is written before a variable,it is reffered to as preincrement/predecrement operators and if they are written after a variable,it is reffered to as post increment/post decrement.
a++ or ++a is equivalent to a = a+1
a- - or - -a is equivalent to a = a-1
Difference between post and pre operators.
| Name | Expression | Explanation |
| Pre-increment | ++a | increment a by 1,then use the new value of a |
| post-increment | a++ | use value of a, then increment a by 1 |
| Pre-decrement | –a | decrement a by 1,then use the new value of a |
| Post-decrement | a– | use value of a, then decrement a by 1 |
Post increment/decrement operator has highest priority than pre-increment/decrement operator.
Precedence of these operators are from right to left.
Example
int a=2,b=3;
int c;
c = ++a - b- – ;
printf(”a = %d,b=%d,c=%d\n”, a,b,c);
Output:
a = 3 , b=2 , c= 0.
Since precedence of the operators is from right to left, first b is evaluated . Since it is post decrement operator,current value of b will be used in the expression i.e. 3 and then b will be decremented by 1(b prints as 2). Then,a is increments to 3(preincrement operator). Therefore,value is evaluated 3 – 3 = 0 which stores to c.




Recent Comments