C++ Programming - References - Discussion

Discussion :: References - Programs (Q.No.2)


Which of the following statement is correct about the program given below?

int main()
    int x = 80; 
    int &y = x;
    cout << x << " " << --y;
    return 0;

[A]. The program will print the output 80 80.
[B]. The program will print the output 81 80.
[C]. The program will print the output 81 81.
[D]. It will result in a compile time error.

Answer: Option A


No answer description available for this question.

Sharath said: (Jun 6, 2012)  
&y means alias of x , so here now x & y are same.

Then cout execution start right to left, So first x++ is 81 then --y is 80
output is 80 80

Ilango said: (Mar 2, 2015)  
&y=x // in this y becomes another name or like a pet name for x.

The --y represents a per-decrement its function is to decrement the value of the variable immediately when declared.

Hence the incremented value x=81 gets decremented by --y since x and y are same.

cout<<x<<" "<<--y // the 'cout' statement prints the values from right to left thus the answer is 80 80.

Demented_Hedgehog said: (Nov 19, 2015)  
There's no sequence point for the << operator so the order of evaluation is unspecified.

Radu said: (Mar 14, 2016)  
First 2 comments (@Sharath and @Ilango) are incorrect.

The associativity of operator "<<" is "left to right" regardless if it's overloaded (like in case of ostream) or not, but that is not related to the evaluation of the scalar value x/y which is undefined (as @Demented_Hedgehog correctly said).

Radu said: (Mar 14, 2016)  
There is no concept of left-to-right or right-to-left evaluation in C++. This is not to be confused with left-to-right and right-to-left associativity of operators: the expression "f1() + f2() + f3()" is parsed as "(f1() + f2()) + f3()" due to left-to-right associativity of operator+, but the function call to f3() may be evaluated first, last, or between f1() or f2() at run time.

The same stands for operator << used in this question.

Radu said: (Mar 14, 2016)  
There are several exceptions to this rule (Ex: For the &&, ||, and, operators) which are noted below.

Sush said: (Jul 31, 2016)  
If there is no associativity involved then A, B options could be right. How can they choose only one?

Srdjan said: (Dec 21, 2017)  
Operator << is left-to-right associative, but operator --(in --y) has greater precedence hence it will be executed sooner. Standard representation of this expression is:

<<(cout, <<(x, <<(" ", --(y)))) //inermost is always executed sooner.

ie) for typical a*b+c:
+(*(a,b), c).

Hatos said: (Feb 27, 2018)  
My output is;

81 80

Release 17.12 rev 11256
g++.exe (GCC) 7.2.0
std = c++1z

Serg said: (Aug 9, 2018)  
Of course, it is 81 80.

Loshi said: (Jul 11, 2019)  
x++means firstly execution then incrementation so the output of x is 80 and after execution, the value of x incremented by 1 that is 81 after that, y read the new value of x that is 81 and --y means decreasing before execution.

After reading the new value of x and perform above operation output of y is 80.

Ak7 said: (Dec 16, 2019)  
1st: cout << x will work after that cout address is returned again and cout<<--y will be printed.

Correct ans is 81 80.

Anant Supekar said: (Aug 14, 2020)  
The correct answer will be 81 80.

Anusha said: (Nov 18, 2020)  
The correct answer is 81 80.

Sid said: (Jul 21, 2021)  
In C++17 answer is 81 80 and in C++ 14 ans is 80 80.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.