C++ Programming - References - Discussion
Discussion Forum : References - Programs (Q.No. 3)
3.
Which of the following statement is correct about the program given below?
#include<iostream.h>
int main()
{
int x = 10;
int &y = x;
x++;
cout<< x << " " << y++;
return 0;
}
Discussion:
14 comments Page 2 of 2.
Atul Kumar said:
1 decade ago
int x = 10; // value of x is 10.
int &y =x; // x value is assigned here to y.
x++ ; // here x becomes 11 and y too .
Now according to cout rule, it(cout) execute form right to left.
So first it will print the value of y i.e., 11 then it will increment the value of y by 1(since y++).
So y as well as x becomes 12.
Hence x will print 12.
int &y =x; // x value is assigned here to y.
x++ ; // here x becomes 11 and y too .
Now according to cout rule, it(cout) execute form right to left.
So first it will print the value of y i.e., 11 then it will increment the value of y by 1(since y++).
So y as well as x becomes 12.
Hence x will print 12.
Somebody said:
1 decade ago
int x = 10; // value of x is 10.
int &y =x; // x value is assigned here to y.
x++ ; // here x becomes 11 and y too .
Then cout rule --> that it executes from right to left.
First y++ executes .its post increment so first y is printed then it is incremented.
y = 11.
Then cout print x . as y is post incremented now .and x takes that increment . and x will print 12.
int &y =x; // x value is assigned here to y.
x++ ; // here x becomes 11 and y too .
Then cout rule --> that it executes from right to left.
First y++ executes .its post increment so first y is printed then it is incremented.
y = 11.
Then cout print x . as y is post incremented now .and x takes that increment . and x will print 12.
Pragyna said:
1 decade ago
Aman y++ is a post increment, since why is a alias for x.
cout<<x<<" "<<y++ in this statement.
You know in cout the execution starts 4m right to left so first y++ is executed and then 'x'since y++ is a post inc. For why it prints 11 and for x prints d incremented value.
cout<<x<<" "<<y++ in this statement.
You know in cout the execution starts 4m right to left so first y++ is executed and then 'x'since y++ is a post inc. For why it prints 11 and for x prints d incremented value.
Aman said:
1 decade ago
How did 12 got printed?
Shouldn't it print 11 11 as the output.
I know the answer is 12 11 only I already cross-checked it on TC. Could someone tell me how this happened?
Shouldn't it print 11 11 as the output.
I know the answer is 12 11 only I already cross-checked it on TC. Could someone tell me how this happened?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers