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.

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)

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

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)

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)

Loshi said:   6 years ago
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.

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

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)

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)


Post your comments here:

Your comments will be displayed after verification.