0

I Cant really undersdand suffix. I know it first uses identifier and then increases or decreses , as first shows i and then ++. But now i think im wrong and still don't understand it.

#include <iostream>

using namespace std;

int main()
{
    int i = 0;
    cout << i << i++ << i;
    cout << "\n\n\nPress Enter to close the window . . . ";
    cin.clear();
    cin.sync();
    cin.get();
    return 0;
}

Output:

101


Press Enter to close the window . . . 

first i is changed before increment readed.Why?

I expected

001

Press Enter to close the window . . .

Can someone explain.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Did you actually read any of the answers in the marked duplicate before accepting the answer? Because the answer you accepted is clearly incorrect. – Alok Save Jun 01 '12 at 14:27

2 Answers2

5

Just never do such a thing, it is undefined

 cout << i << i++ << i;

better do

 cout << i << i << (i + 1);
 i ++;

if you want your expected result.


The case

  cout << i++;

is defined and perfectly ok.

stefan bachert
  • 9,413
  • 4
  • 33
  • 40
-1

I think what is undefined*) here is the order of evaluation of function arguments. What you are actually calling here are function calls to the (overloaded)

std::ostream& operator<< (std::ostream&, int);

and the first argument is the output of another call to the same function, so your

cout << i << i++ << i;

expands to

operator<<( operator<<( operator<<(cout,i), i++), i);

As the order in which function arguments are evaluated is not specified, anything can happen here. You can avoid that by writing separate lines:

cout << i;
cout << i++;
cout << i;

which expands into the harmless

operator<<(cout,i);
operator<<(cout,i++);
operator<<(cout,i);

*) edit: to be more precise, the cout<<i<<i++; is undefined because the order of evaluation of function arguments in unspecified.

Walter
  • 44,150
  • 20
  • 113
  • 196
  • 2
    -1 Order of evaluation of arguments to a function is not Undefined, it is Unspecified. Unspecified & Undefined are not the same.Also, your description does not explain correctly why this is Undefined Behavior. Check the answers in the marked duplicate to know exactly this is Undefined Behavior. – Alok Save Jun 01 '12 at 14:20
  • @Als why do you criticise me instead of edit and correct? I thought this site is for mutual help not a race for being more clever than others. btw, I obviously didn't follow the duplication link. – Walter Jun 01 '12 at 14:27
  • 2
    Are you expecting me to edit and correct your incorrect answer when i already answered this in very much detail [here](http://stackoverflow.com/a/10782963/452307) Since you don't understand you should ask and resolve your doubts not post incorrect answers.Downvoting is a means to indicate incorrectness or displeasure towards an answer.I downvoted your answer because it is incorrect.Lastly, If you can't take criticism don't post. FYI your answer is still Incorrect. – Alok Save Jun 01 '12 at 14:36
  • @Als why do you think I cannot take criticism? – Walter Jun 01 '12 at 15:15