pratik jain
pratik jain

Reputation: 49

I don't know how we got output 0 5 0 of the following program

main()
{
   int k=35;
   printf("%d %d %d",k==35,k=50,k>40);
}

The output of this code is 0 50 and 0 I couldn't understand why? Because I have learnt that priority of assignment operator is the least and the releational operator priority is more that that then how this kind of output has come please explain?

Upvotes: 2

Views: 68

Answers (1)

Abhishek Jaiswal
Abhishek Jaiswal

Reputation: 308

There is no fixed order defined by the C standard. A compiler may choose to evaluate either left-to-right or right-to-left. So in your case, the compiler is evaluating the expression from right to left. And as you know that == generates the output as a boolean value. So the first evaluation done by the compiler is 35>40 which is False(0) and k=50 so simply assignment is done and finally, k(50) is compared with 35 which results in False(0).

Therefore your output is 0 50 0.

I hope it cleared your doubt..

Upvotes: 1

Related Questions