0

If C Compiler works from left to right then why is the following code's output in a way as if it is executing from right to left?

int x = 15 ; 
printf("%d %d %d", x!=15, x=20, x<30); 

Output : 1 20 1

Vaishali
  • 131
  • 9
  • 2
    No, the order is unspecified. What you're doing is *undefined behaviour*. – P.P Aug 28 '18 at 09:04
  • 1
    Just to clear up any potential confusion - they may be executed in a certain order on your system, but they may execute in any other order on another computer or with another compiler. – Prof. Falken Aug 28 '18 at 09:08
  • "If C Compiler works from left to right then why...?" This is like saying "If money is free then why when I went to the bank and asked for some they didn't give me any?" :-) – Steve Summit Aug 28 '18 at 10:34

1 Answers1

2

First of all, those are expressions and not statements.

Secondly, the order of argument evaluation is unspecified. You can't tell in what order the arguments will be evaluated, and your code would lead to undefined behavior.

For more information about evaluation order and sequencing see e.g. this reference.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621