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

I was studying C and solving a pseudocode. I tried to solve it and my answer was 0 20 1, but when I tried to compile and check the answer was 1 20 1. Can someone explain me why this is happening?

When I tried to interchange the condition to printf ("\n%d %d %d", x = 20,x != 15, x < 30 ); and here I got answer to be 20 0 1

Why is this happening?

Ayush Bali
  • 21
  • 2
  • 7
  • This isn't just an order of evaluation issue. `x = 20` is unsequenced in relation to the unrelated value access of `x` in the other arguments. This code invokes undefined behavior. – Lundin Sep 10 '20 at 09:55

1 Answers1

-1

The order of evaluation of function argument is unspecified. So, if you have an expression which alters the value of one variable, which appears as other arguments in a conditional check, there's no guarantee for the result.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261