i found a very simple program that is very simple but have a great logic.

#include<stdio.h>
#include<conio.h>
void main()
{
int B=1;
printf("%d\n%d\n%d",b=15,b>9,b<9);
getch();
}

this seems very simple to me but the output is very strange to me

OUTPUT:-

15
0
1

I tried very much to understand this but i can't..
So please help me in understanding it...

Recommended Answers

All 7 Replies

1 mins true (b=1 so b<9 is true)
0 mins false

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.

I dont understand it....

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.........

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.........

i get this but why it happens only in case of printf???

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 reread Narue'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?

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 than and 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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.