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
iis evaluated before the increment. Because it's0the second part of the expression no longer needs to be evaluated,0 && 0is0,0 && 1is also0.The same expression with a pre-increment means that
iwill be1when it's evaluated, in this case the second part of the expression is important, because1 && 1is possible, but1 && 0is also possible, and these will render different results.