-4
#include<stdio.h>
int main()
{
    int var = 10 ; 
    printf ( "\n%d %d",var==10, var = 100) ;
}

Output:

0 100

Inside the printf statement, var==10 evaluates to true but then I'm getting the output as 0. Why does this happen?

Haris
  • 11,989
  • 6
  • 41
  • 67
  • See: [Why are these constructs (using ++) undefined behavior?](http://stackoverflow.com/questions/949433/why-are-these-constructs-using-undefined-behavior) – P.P Dec 16 '15 at 17:02

1 Answers1

4

You're modifying var inside of the function call. Parameters to a function can be evaluated in any order. In this particular example, var = 100 is being evaluated before var==10, but there's no guarantee the behavior will be the same if you use a different compiler.

Because you're attempting to read and modify a variable in the same expression without a sequence point to separate them, you're invoking undefined behavior.

dbush
  • 186,650
  • 20
  • 189
  • 240