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;
}
Discussion:
17 comments Page 1 of 2.
Radu said:
9 years ago
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.
The same stands for operator << used in this question.
Ilango said:
1 decade ago
&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.
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.
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).
<<(cout, <<(x, <<(" ", --(y)))) //inermost is always executed sooner.
ie) for typical a*b+c:
+(*(a,b), c).
(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.
After reading the new value of x and perform above operation output of y is 80.
Radu said:
9 years ago
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).
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).
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
Then cout execution start right to left, So first x++ is 81 then --y is 80
output is 80 80
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;".
Here, the correct answer is D since you're using "cout" and you didn't declare beforehand that you are "using namespace std;".
(5)
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.
Correct ans is 81 80.
(1)
Radu said:
9 years ago
There are several exceptions to this rule (Ex: For the &&, ||, and, operators) which are noted below.
Sush said:
9 years ago
If there is no associativity involved then A, B options could be right. How can they choose only one?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers