What is the result i + ++ i?

Possible duplicate:
Could anyone explain these undefined behaviors (i = i ++ + ++ i, i = i ++, etc ...)

Why does this code generate 8?

#include <iostream> 
using namespace  std ;
void myFunction(int i)
{
    i = i + 2 + ++i;
    cout<<i<<endl;
}

void main () 
{
    int i = 2;
    myFunction(i);
    cin>> i;
}

I think the result should be 7 not 8 ... I am using Visual Studio 2008

+3
source share
5 answers

The order in which members are evaluated on the right side of this expression

i = i + 2 + ++i;

- undefined. that is, they can occur in any order. In this case, the compiler decided to increase I first (++ i, third term) before evaluating I (first term), which leads to 3 + 2 + 3.

+18
source

i , , . undefined, .

+13

. . .

+4

++ , i + 2 + ++i ( = 2) 3 + 2 + 3, 8.

-1

"++ i". "i" 3, 3 + 2 + 3 = 8. , !

-1

Source: https://habr.com/ru/post/1772720/


All Articles