C++ Programming - References - Discussion

Discussion Forum : References - Programs (Q.No. 2)
2.
Which of the following statement is correct about the program given below?
#include<iostream.h> 
int main()
{
    int x = 80; 
    int &y = x;
    x++;
    cout << x << " " << --y;
    return 0;
}
The program will print the output 80 80.
The program will print the output 81 80.
The program will print the output 81 81.
It will result in a compile time error.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
17 comments Page 1 of 2.

Bhavesh Garg said:   2 years ago
The Correct Answer is (B) 81 80.
(15)

Blake said:   2 years ago
@All.

Here, the correct answer is D since you're using "cout" and you didn't declare beforehand that you are "using namespace std;".
(5)

Sid said:   4 years ago
In C++17 answer is 81 80 and in C++ 14 ans is 80 80.
(4)

Anusha said:   5 years ago
The correct answer is 81 80.
(4)

Anant Supekar said:   5 years ago
The correct answer will be 81 80.
(3)

Hatos said:   8 years ago
My output is;

81 80

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

Ak7 said:   6 years ago
1st: cout << x will work after that cout address is returned again and cout<<--y will be printed.

Correct ans is 81 80.
(1)

Srdjan said:   8 years ago
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).
(1)

Sharath said:   1 decade ago
&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

Serg said:   7 years ago
Of course, it is 81 80.


Post your comments here:

Your comments will be displayed after verification.