main() { int i=5; printf(“%d†,i=++i ==6); }
the output is 1.why???
Collapse
X
-
I don't believe there is any undefined behavior here. Your snippet is equivalent to
The output of the == operator is either 1 or 0. In this case, it is 1 because ++i is equal to 6.Code:main() { int i = 5; i = (++i == 6); printf(“%dâ€, i); }
I assume the odd format string is a cut-and-paste artifact. By the way, the proper return value of main is int.Comment
-
Folks,
All constructs of the form
are in the realm of undefined behaviour according to the language specification.Code:i = i++;
Basically all expressions have undefined evaluation order, so an expression that has two side effects on a variable is ambiguous.Comment
Comment