C++ Programming - References - Discussion
Discussion Forum : References - Programs (Q.No. 4)
4.
Which of the following statement is correct about the program given below?
#include<iostream.h>
int main()
{
int x = 10;
int &y = x;
x = 25;
y = 50;
cout<< x << " " << --y;
return 0;
}
Discussion:
13 comments Page 1 of 2.
Ovidiu said:
4 years ago
Starting from the standard c++17 result will be:
50 49.
In this standard, they changed std::cout.
50 49.
In this standard, they changed std::cout.
Anusha said:
5 years ago
The answer is 50 49 as x is printed first its value is 50 and then why is decremented.
Anant Supekar said:
5 years ago
The correct answer will be 50 49.
Abhishek said:
5 years ago
Yes, I agree, The answer will be 50 49.
Kjell said:
8 years ago
What compiler are you using?
I verified it with Apple LLVM 8.0 and g++ 5.4.0:
1. the result is 50 49,
2. #include <iostream.h> is not valid since a long time, it must be #include <iostream>
<< Is evaluated left to right, the decrement will happen after x was already streamed.
I verified it with Apple LLVM 8.0 and g++ 5.4.0:
1. the result is 50 49,
2. #include <iostream.h> is not valid since a long time, it must be #include <iostream>
<< Is evaluated left to right, the decrement will happen after x was already streamed.
Meenakshi L said:
9 years ago
Is y = x same as &y=x?
As you said before the value of x is stored in y. So, we can use y=x is enough.
Why we need to use &y=x?
As you said before the value of x is stored in y. So, we can use y=x is enough.
Why we need to use &y=x?
Muhammad Mazhar said:
9 years ago
There is a confusion that the value of x is printed first, then -- decrement the value of y and print.
According to this, the value should be 50 49.
According to this, the value should be 50 49.
Bhawna said:
10 years ago
If we create an integer variable and then create a reference which refers to this, any mention of the reference will be exactly the same as a mention to original variable.
Asaad said:
10 years ago
I can also add that x takes the value of y and also y takes the value of x. This appears when you put y=50;
And then x=25;
The output will become 24 24.
Just try it.
And then x=25;
The output will become 24 24.
Just try it.
Vikas said:
10 years ago
Because here value of x is assigned in y. So every change in y also affect value of x.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers