The output is undefined because commas in a function argument list don't act as sequence points. The rule states that if you modify an object, it may be modified only once between sequence points and only accessed to determine the new value. Your printf() statement breaks the latter part of that rule.
Even if it weren't undefined, the behavior would be unspecified because functions are allowed to push arguments in any order. The output could just as easily be 15,1,0 as 15,0,1 depending on whether the b=15 expression is pushed first or last.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
the parameters taking of printf function is from right to left.....
So in the given problem the function executes as follows....
b=1;
printf("\n %d %d %d",b=15, b>9, b<9);
So first it executes the condition b<9 but here b=1
next it executes the condition b>9. Here also b=1
and the last step is b=15 will executes.........
Please rereadNarue's post. She is correct, you are mistaken.
i get this but why it happens only in case of printf???
What makes you think it only happens with printf?
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
the parameters taking of printf function is from right to left.....
Don't confuse how C works on your implementation with how C is specified to work. There's no requirement that parameters be pushed right to left. They could be pushed left to right, or evens then odds, or staggered using the Fibonacci sequence (though right to left and left to right are the most common ;)).So first it executes the condition b<9 but here b=1
next it executes the condition b>9. Here also b=1
How can b be both less thanand greater than 9? Are you using New Math?
i get this but why it happens only in case of printf???
It doesn't happen only with printf():
#include <stdio.h>
void foo(int a, int b, int c)
{
printf("a = %d\n", a);
printf("b = %d\n", b);
printf("c = %d\n", c);
}
int main(void)
{
int val = 1;
/* Beware! Here be dragons (nasal demons) */
foo(val = 15, val > 9, val < 9);
return 0;
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401