Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between i = ++i and ++i [duplicate]

Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)

What is the difference between i = ++i; and ++i; where i is an integer with value 10?

According to me both do the same job of incrementing i i.e after completion of both the expressions i =11.

like image 801
Jacob Martin Avatar asked Oct 12 '10 11:10

Jacob Martin


People also ask

Is duplicate same as original?

Unlike a copy, usually to make a duplicate, you need the original. That is because a duplicate is an identical copy or an exact reproduction of the original.

What does duplicate mean on iPhone?

It means that you can make exactly the same copy of the photo or image right to your iPhone, two copies of the exact same photo. That way you can edit now edit the duplicated copy without having to worry about making changes on the original copy, thereby preserving it in its original format.

What is difference between replicate and duplicate?

The word duplicate is derived from the Latin word duplicare, which means to double. Replicate means to reproduce something, to construct a copy of something, to make a facsimile. The word replicate may be interchangeable with the word duplicate except in a few instances.

What does MAC duplicate do?

Duplicate is almost the same as Copy, except that the copy is created in the same location as the original and assigned a new name. To do this, choose the Duplicate command (Command-D) from the Finder's File menu. The copy will have the word copy appended to its name.


3 Answers

i = ++i; invokes Undefined Behaviour whereas ++i; does not.

C++03 [Section 5/4] says 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.

In i = ++i i is being modified twice[pre-increment and assignment] without any intervening sequence point so the behaviour is Undefined in C as well as in C++.

However i = ++i is well defined in C++0x :)

like image 143
Prasoon Saurav Avatar answered Oct 09 '22 06:10

Prasoon Saurav


Writing i = ++i; writes to variable i twice (one for the increment, one for the assignment) without a sequence point between the two. This, according to the C language standard causes undefined behavior.

This means the compiler is free to implement i = ++i as identical to i = i + 1, as i = i + 2 (this actually makes sense in certain pipeline- and cache-related circumstances), or as format C:\ (silly, but technically allowed by the standard).

like image 33
Victor Nicollet Avatar answered Oct 09 '22 05:10

Victor Nicollet


i = ++i will often, but not necessarily, give the result of

i = i;

i +1;

which gives i = 10

As pointed out by the comments, this is undefined behaviour and should never be relied on

while ++i will ALWAYS give

i = i+1;

which gives i = 11;

And is therefore the correct way of doing it

like image 31
kskjon Avatar answered Oct 09 '22 04:10

kskjon