Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

Possible Duplicates:
What is more efficient i++ or ++i?
How do we explain the result of the expression (++x)+(++x)+(++x)?
Difference between i++ and ++i in a loop?

I am trying these two programs:

void fun(){
     int k = 0;
     int i= 10;
     k = (i++)+(++i);
     cout << k << endl;
}

Output = 22 as i++ will give 10 and ++i will evaluate into 12.

But

void fun(){
     int k = 0;
     int i = 10;
     k = (++i)+(++i);
     cout << k << endl;
}

Output = 24

It should be 23 I guess, or is there something that I am not able to see?

like image 582
aR. Avatar asked Nov 05 '09 05:11

aR.


People also ask

What is the difference between duplicate and duplicate?

Duplicate may be used as a noun, verb or adjective. Related words are duplicates, duplicated, duplicating, duplication. 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.

What does one duplicate mean?

: either of two things exactly alike and usually produced at the same time or by the same process. : an additional copy of something (such as a book or stamp) already in a collection. : one that resembles or corresponds to another : counterpart. 3. : two identical copies.

What does MAC duplicate do?

By default, the File menu shows Duplicate below the Save item. Choosing File > Duplicate creates a copy of the current state of the file, including unsaved changes. The copy is a separate document that has as its name the original file's title plus the word “Copy,” and the title is highlighted.

What is a duplicate document?

Duplicate Document means a written counterpart of the original produced by the same impression as the original or from the same matrix or by digitized electronic transmission, readable by sight, which accurately reproduces the original.(B) Filing by Electronic Facsimile Transmission.


1 Answers

Note: you are invoking undefined behavior (modifying a variable twice between sequence points)

like image 89
Brian Avatar answered Oct 15 '22 11:10

Brian