c - Explaination for printf with comparing variables as arguments -
main(){ int = 5; int b = 6; printf("%d %d %d",a==b,a=b,a<b); }
output in testing
1 6 1
in above program expecting output 0 6 0 . in compilers giving output (e.g. xcode) in other compilers giving output 1 6 1 . couldn't find explanation . case of sequence point.
consider below program
main(){ int = 5; int b = 6; printf("%d %d %d",a<b,a>b,a=b); printf("%d %d",a<=b,a!=b); }
output in testing
0 0 6 1 0
this below program giving correct output expecting 0 0 6 1 0 why above program not giving output 060 in of compilers
c standard says:
c11: 6.5 (p2):
if side effect on scalar object unsequenced relative to either different side effect on same scalar object or a value computation using value of same scalar object, behavior undefined [...]
this means program invokes undefined behavior. in statements
printf("%d %d %d",a==b,a=b,a<b);
and
printf("%d %d %d",a<b,a>b,a=b);
the side effect a
unsequenced because standard says:
6.5.2.2 (p10):
[...] every evaluation in calling function (including other function calls) not otherwise sequenced before or after execution of body of called function indeterminately sequenced respect execution of called function.
Comments
Post a Comment