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