int main() {
int a = -1, b = 1, c = 1, d = 0, e = 2, f = 2;
int z = f-- && e++ && d++ && c-- || b++ || a++;
cout << a << b << c << d << e << f << z << endl;
return 0;
}
The output I got is different from what I am expected.
Why is the value of c 1 instead of 0?
I assumed that (f-- && e++ && d++ && c--) will be evaluated (from left to right) to give false and update all the values of the variables. Then, as b++ ensure that the statement is true, a++ is not evaluated.
will be evaluated (from left to right) to give false
Yes. And it will stop as soon as value of logic expression is determined due to short-circuiting property of logical operators. In this case after evaluating d++ to 0 program do not need to evaluate value of c--: false && anything == false.