I bumped into a problem which prompted me to do some research. I have found that a piece of code like this:
#include <stdio.h>
int main(void)
{
char i = 0;
i++ && puts("Hi!");
printf("%hhd\n", i);
}
only processes the increment, and outputs:
1
That is not the case if the postfix increment is replaced by a prefix one, it outputs:
Hi!
1
Why does it behave like that?
I apologise if the question is dumb.
In
i
is evaluated before the increment. Because it's0
the second part of the expression no longer needs to be evaluated,0 && 0
is0
,0 && 1
is also0
.The same expression with a pre-increment means that
i
will be1
when it's evaluated, in this case the second part of the expression is important, because1 && 1
is possible, but1 && 0
is also possible, and these will render different results.