Question

Can someone explain to me why this code prints 14? I was just asked by another student and couldn't figure it out.

int i = 5;
i = ++i + ++i;
cout<<i;
Was it helpful?

Solution

The order of side effects is undefined in C++. Additionally, modifying a variable twice in a single expression has no defined behavior (See the C++ standard, §5.0.4, physical page 87 / logical page 73).

Solution: Don't use side effects in complex expression, don't use more than one in simple ones. And it does not hurt to enable all the warnings the compiler can give you: Adding -Wall(gcc) or /Wall /W4(Visual C++) to the command line yields a fitting warning:

test-so-side-effects.c: In function 'main':
test-so-side-effects.c:5: warning: operation on 'i' may be undefined
test-so-side-effects.c:5: warning: operation on 'i' may be undefined

Obviously, the code compiles to:

i = i + 1;
i = i + 1;
i = i + i;

OTHER TIPS

That's undefined behaviour, the result will vary depending on the compiler you use. See, for example, C++ FAQ Lite.

In some of the answers/comments there has been a discussion about the meaning of 'undefined behavior' and whether that makes the program invalid. So I'm posting this rather long answer detailing exactly what the standard says with some notes. I hope it's not too boring...

The quoted bits of the standard come from the current C++ standard (ISO/IEC 14882:2003). There's similar wording in the C standard.

According to the C++ standard, modifying a value more than once within a set of sequence points results in undefined behavior (section 5 Paragraph 4):

Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified.53) Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined. [Example:

i = v[i++]; // the behavior is unspecified
i = 7, i++, i++; // i becomes 9
i = ++i + 1; // the behavior is unspecified
i = i + 1; // the value of i is incremented

—end example]

Note that the second example, "i = 7, i++, i++;" is defined since the comma operator is a sequence point.

Here's what the C++ standard says 'undefined behavior' means:

1.3.12 undefined behavior [defns.undefined]

behavior, such as might arise upon use of an erroneous program construct or erroneous data, for which this International Standard imposes no requirements. Undefined behavior may also be expected when this International Standard omits the description of any explicit definition of behavior. [Note: permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message). Many erroneous program constructs do not engender undefined behavior; they are required to be diagnosed. ]

In other words, the compiler is free to do whatever it wants, including

  1. spitting out an error message,
  2. doing something implementation defined & documented,
  3. having completely unpredictable results

The second item covers language extensions which most compilers have, but of course are not defined in the standard.

So I guess that strictly speaking something that exhibits undefined behavior is not 'illegal', but in my experience whenever there's been something in a C/C++ program that exhibits 'undefined behavior' (unless it's an extension) - it's a bug. I think that calling such a construct illegal is not confusing, misleading, or misguided.

Also, I think trying to explain what the compiler is doing to reach the value 14 is not particularly helpful, as that misses the point. The compiler could be doing almost anything; in fact, it's likely that the compiler may reach a different result when it's run using differing optimization options (or may produce code that crashes - who knows?).

For those who want some additional references or an appeal to authority, here are some pointers:

Steve Summit's (maintainer of the comp.lang.c Frequently Asked Questions) long, long answer on this topic from 1995:

Here's what Bjarne Stroustrup has to say on the matter:


Footnote: the C++ standard uses the word 'illegal' exactly once - when describing a difference between C++ and Standard C regarding the use of static or extern with type declarations.

Simple... you compiler is evaluating BOTH increments before performing the sum, without caching the intermediate results. This means that when you add i twice, it now has the value of 7.

If you do

int j=++i; 
int k=++i;

i = j+k;

you'll see 13 as expected.

On your particular compiler, it's choosing to do both of the ++ operations first, then the addition. It's interpreting the code as:

int i = 5;
++i;
++i;
i = i + i;
cout << i;

That's perfectly valid.

A better question would be, is it always going to be 14?

int i = 5;
i = ++i + ++i;
cout<<i;

i = ++i   + ++i   ;
i = ++(5) + ++(5) ;
i =    6  +    6  ;
i = 12;

i = ++i   + ++i   ;
i = ++i   + ++(5) ;
i = ++i   +   (6) ;
i = ++(6) +    6  ;
i =   (7) +    6  ;
i = 13;

i = ++i   + ++i   ;
i = ++i   + ++(5) ;
i = ++(6) +   (6) ;
i =   (7) +   (7) ;
i = 14;

In all likelihood it will probably be 14, because it makes slightly more sense.

I think that when looking at the problem from the sight of the syntax tree, the answer to the problem becomes clearer:

i
|
=
|
+
|
unary expression - unary expression

unary expression: unary operator expression

In our case expression boils down to the variable i.

Now what happens is that both unary expression modify the same operand, so the code does two times ++i when evaluating the unary expressions before adding the results of both unary expressions.

So what the code does is indeed

++i;
++i;
i = i + i;

For i = 5 that means

i = i + 1; //i <- 6
i = i + 1; //i <- 7
i = i + i; //i <- 14

Because the prefix increment has precedence:

int i = 5;
i = i+1; // First ++i, i is now 6
i = i+1; // Second ++i, i is now 7
i = i + i // i = 7 + 7
cout << i // i = 14
 i = i++ + i; //11  

 i = i++ + i++; //12

 i = i++ + ++i; //13

 i = ++i + i++; //13

 i = ++i + ++i; //14    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top