-1
#include<iostream>
using namespace std;

int main(){

 char c = 'a';
 int numb;

 for (int i = 1; i <= 25 ; i++){

    cout << c <<   "," << c++ << endl;

 }

}

Why is it that when I print the output above, the following will get printed:

b,a

c,b

d,c

But I was expecting:

a,b

b,c

c,d
jonrsharpe
  • 99,167
  • 19
  • 183
  • 334
john_w
  • 545
  • 1
  • 4
  • 19
  • It's up to the compiler to determine which order to do this in. [cout << order of call to functions it prints?](https://stackoverflow.com/questions/2129230/cout-order-of-call-to-functions-it-prints) has a detailed answer that should satisfy the question. – Kai Feb 25 '20 at 23:27
  • 1
    You probably meant `++c`. As-is, on a C++17 compiler the output will be `a,a`, `b,b` etc. Explore the [order of evaluation](https://en.cppreference.com/w/cpp/language/eval_order) reference. – Ron Feb 25 '20 at 23:33
  • A bit of a guess here. So ++ has higher operator precedence than <<. a="" already.="" am="" an="" as="" because="" been="" but="" c="" compiler="" described.="" done="" first="" first.="" gets="" guessing="" guy="" has="" however="" i="" incremented="" is="" it="" not="" operation.="" operator="" post-increment="" precedence="" pretty="" so="" sure="" the="" then="" thing=""> – Frank Merrow Feb 25 '20 at 23:36
  • @FrankMerrow the `< – user4581301 Feb 25 '20 at 23:50
  • This has [nothing to do with operator precedence](https://stackoverflow.com/a/58976592/4386278), @Frank. – Asteroids With Wings Feb 25 '20 at 23:58

2 Answers2

1

If you are not compiling to the C++17 Standard revision or more recent you have encountered undefined behaviour. The older C++ Standards do not specify the sequencing of

cout << c <<   "," << c++ << endl;

so there are no guarantees on when that c++ will occur. The only thing you can count on is the c++ term will be the initial value because ++ increments after the value is collected.

a,a

or

b,a

are valid outputs.

As of C++17 the Standard guarantees that all side effects will be resolved before proceeding to the next <<. << c will be resolved, not that there is much to resolve, before << "," starts. << c++ comes even later in the chain. This means you should always see

a,a 
b,b
c,c

See the notes on Undefined Behaviour at the bottom of Order of evaluation

user4581301
  • 29,019
  • 5
  • 26
  • 45
  • hello, could you explain what you mean by "c++ is guaranteed to sequence after << c" ? could you provide an example of what that means? thank you – john_w Feb 25 '20 at 23:59
  • @john_w Your question is a near-perfect example, so I've tried to update the answer to better describe why the sequencing is now fixed. – user4581301 Feb 26 '20 at 00:38
0

I believe that it has to do with the order of operations here. The stream operator (<<) operates from right to left. Meaning that in the first run, c++ evaluates to "a", but causes c to be iterated up to "b".

  • 2
    This is actually a nasty case. In older compiler standards (before C++17) there are no firm guarantees on when that `c++` will occur. – user4581301 Feb 25 '20 at 23:25