What does an assignment return?

It evaluates to 2 because that’s how the standard defines it. From C11 Standard, section 6.5.16:

An assignment expression has the value of the left operand after the assignment

It’s to allow things like this:

a = b = c;

(although there’s some debate as to whether code like that is a good thing or not.)

Incidentally, this behaviour is replicated in Java (and I would bet that it’s the same in C# too).

Leave a Comment