booleans and evaluation order

Hi, I am new to C and C++.

1
2
3
4
5
6
7

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.

Thanks in advance!
Last edited on
I think it is the error in your compiler. My Borland C++ Builder works for c correctly - its value is 1.
Last edited on
Sorry, I meant why is it 1 instead of 0? Why isn't c-- 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.
Topic archived. No new replies allowed.