What is the result of i == (i = 2)? What is the result of i == (i = 2)? c c

What is the result of i == (i = 2)?


The behaviour of a C program that executes the expression i == (i = 2) is undefined.

It comes from C11 6.5p22:

  1. If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.84)

The i on the left-hand side of == is a value computation on the value of scalar object i and the right-hand side i = 2 has a side effect of assigning the value 2 to i. The LHS and RHS of == are unsequenced w.r.t. each other. Hence the entire program is meaningless in C.

Compile with gcc -Wall and GCC will spit out:

unsequenced.c:5:16: warning: operation on ‘i’ may be undefined [-Wsequence-point]     if(i == (i = 2)) {             ~~~^~~~

Unlike C, Java guarantees the evaluation order for operands (left-to-right), therefore

haveNext = (prev == (prev = get()));

is correct in Java. The value of LHS is determined strictly before the evaluation of the side effect on the RHS occurs.

In C you have to write this as something like

newPrev = get();haveNext = (prev == newPrev);prev = newPrev;


The Java Language Specification (§15.7) states:

The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

The specification (§15.21.1) also states that:

The value produced by the == operator is true if the value of the left-hand operand is equal to the value of the right-hand operand; otherwise, the result is false.

Therefore in Java, the if-statement at runtime would look like the following, which obviously evaluates to false:

if (1 == 2) {}

In C, it is simply undefined (see Antti's answer).


In C, the behavior of i == (i = 2) is undefined because it attempts to both update an object and use that object’s value in a computation without an intervening sequence point. The result will vary based on the compiler, compiler settings, even the surrounding code.