-2

So this question is basically guess the output program. I will be specific.

What I understood is value of x is passed by reference to y, which means whatever changes we do on x is reflected on y and vice versa. In line 6 we pass the value of x to y by pass by reference.

In line 7, x is incremented, so x becomes 11 and so does y. Now I expected the output to be 11 11, because we are printing x first, so x which was 11 will be printed , and as of y, it is post increment so 11 will be printed first and then only y is incremented. But I was wrong(obvious) the answer was 12 11.

I tried debugging and it didn't explain why it happened either. After some googling, I found that there is something called cout rule where execution of cout statement happens from right to left, which means in line 8, due to post increment y is printed first, and then incremented so x gets changed and becomes 12. But again the output in that case is 11 12 and not 12 11. Please can you explain if the cout rule is really true and if yes can you explain how is it really done or there is something else to this? Thanks.

#include <iostream>
using namespace std;
int main()
{
    int x = 10;
    int &y = x;             // line 6
    x++;                    // line 7
    cout<< x << " " << y++; // line 8
    return 0; 
}
Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
ilovepie
  • 3
  • 2
  • Well yes, it looks like it actually processed your data from right to left while your output is still according to your `cout` format. – ionizer Dec 10 '17 at 15:46
  • @user0042 I don't understand. Is this comment supposed to mock my detailed question? I tried my level best explaining my question. It may sound silly but there's certainly learning in this question, at least for me. – ilovepie Dec 10 '17 at 15:46
  • @ionizer is the execution of the cout statement always from right to left or this is treated as a special case? – ilovepie Dec 10 '17 at 15:47
  • @ilovepie _"at least for me"_ Qualifies for one of the _least reasons_ to ask here. – user0042 Dec 10 '17 at 15:48
  • @user0042 Well even if your answer was a mere mockery , I will take that as an advice and move on. Thanks for taking your time in pointing out the the inefficiency of my question. – ilovepie Dec 10 '17 at 15:52
  • @ilovepie You should note that this was intentionally a comment and not an _answer_ by means to this site. I noticed you mentioned _debugging_. I have no clue how you actually did that. But the evaluation order of the statements in `cout << ...` is undefined. – user0042 Dec 10 '17 at 15:56
  • @user0042 you mean to say that, it isn't wrong to guess the output as 11 12? Or according to you what would be the answer 11 12 or 12 11? Please help – ilovepie Dec 10 '17 at 15:58
  • Only a guess, but I think it's because your compiler puts your `cout` and its parameters into a single execution stack or if it's what you call it, and process data before it actually outputs. Different compiler might yield different result also. – ionizer Dec 10 '17 at 16:00

1 Answers1

0

Some compilers will print 11 11

while others will print 12 11

The standard doesn't define the order in such as cases.

I'm still don't understand why you used the reference? it doesn't change anything, it just like the following:

#include<iostream> 
using namespace std;
int main()
{
  int x = 10;       
  x++;       
  cout<< x << " " << x++;         
  return 0; 
}
SHR
  • 7,940
  • 9
  • 38
  • 57
  • I didn't use anything. This is an internet MCQ question and the options were: A. The program will print the output 11 12. B. The program will print the output 12 11. C. The program will print the output 12 13. D. It will result in a compile time error.correct option was B This is the link of the site, 3rd question : https://www.indiabix.com/cpp-programming/references/005001 – ilovepie Dec 10 '17 at 15:59
  • 1
    _"This is an internet MCQ question ..."_ Just skip it. It's not answerable in a reasonable way. – user0042 Dec 10 '17 at 16:08
  • 2
    @ilovepie: What's an "MCQ" question anyway? Not that I care much - it's apparently a useless kind of question when *all* options are wrong. – Christian Hackl Dec 10 '17 at 16:33
  • Just avoid that entire site - I looked at a few of the questions and they are atrocious - many have no correct answer, and even for those that do the questions are pretty idiotic. Whoever compiled the questions evidently knows very little about C++. – Paul R Dec 11 '17 at 09:59