I have executed the following code in Code::Blocks 10.05 on Windows 7.
int a=0,b=0,c;
c=a++&&b++;
printf("\na=%d\nb=%d\nc=%d\n\n",a,b,c);
The output I obtained is given below,
a=1
b=0
c=0
This makes perfect sense because of short circuit evaluation.
The expression a++
is post increment and 0
is returned to the logical and (&&
). Hence the part b++
is not evaluated since both 0 && 0
and
0 && 1
evaluates to 0
.
But here arises my doubt. The precedence value of operators clearly states that ++
is having higher precedence over &&
. So my understanding was like this, both a++
and b++ are evaluated and then &&
only checks the result of expression a++
to come to a decision. But this has not happened only a++
is evaluated here.
What is the reason for this behavior? Does &&
being a sequence point has something to do with this behavior? If so why we say that &&
is having lower precedence than ++
?
a++
will increment at some indeterminate time between whena
is evaluated and when the statement finishes. That's why you can't usea
again within the same expression (before or after thea++
). – Judges