The following code has me confused
int a=2,b=5,c; c=a+++b; printf("%d,%d,%d",a,b,c);
I expected the output to be 3,5,8, mainly because a++ means 2 +1 which equals 3, and 3 + 5 equals 8, so I expected 3,5,8. It turns out that the result is 3,5,7. Can someone explain why this is the case?
In the C Programming Language, the abs function returns the absolute value of an integer.
C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.
a<<b for integers means "shift left". The bitwise representation of a is shifted left b bits. This is the same as multiplying by (2 to the power of b ).
It's parsed as c = a++ + b
, and a++
means post-increment, i.e. increment after taking the value of a
to compute a + b == 2 + 5
.
Please, never write code like this.
Maximal Munch Rule applies to such expression, according to which, the expression is parsed as:
c = a++ + b;
That is, a
is post-incremented (a++
) and so the current value of a
(before post-increment) is taken for +
operation with b
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With