in above program i am expecting output as 0 6 0
You are expecting argument evaluation from left to right (reading direction of english). But the order of evaluation may go from left to right, right to left or in some other order. A program should not depend on the order of evaluation of function arguments.
printf("%d %d %d",a==b,a=b,a<b);
// 1. Evaluate the following in no specific order
// (a==b), (a=b) and side effects, (a<b)
// (Warning: Depending on the order, result may be different)
// 2. Pass "%d %d %d" as first argument, the result of
// (a==b) as second argument, result of of (a=b) as
// third argument and result of (a<b) as fourth
// argument of printf
In C
order of argument evaluation is unspecified. So it can evaluate the arguments in any order. As you are changing the value of variable a
(using assignation operator =
) in one of the argument, use the following for more consistent result.
printf("%d ",a==b); // Evaluate (a==b) and pass as second argument of printf
printf("%d ",a=b); // Evaluate (a=b) and side effects and pass as second argument of printf
printf("%d" ,a<b); // Evaluate (a==b) and pass as second argument of printf
Without this your program invokes undefined behavior because you are updating and reading the value of a
before the sequence point. But in latter case there is no ambiguity and thus program shows a well defined behavior.
p.s. Don't confuse between comma used in function parameter list and the comma operator