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; 
}
The program will print the output 11 12.
The program will print the output 11 11.
The program will print the output 12 13.
It will result in a compile time error.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
14 comments Page 1 of 2.

Swetha said:   6 years ago
The output is :11 11.
(4)

Anusha said:   5 years ago
The output is 11 11.
(4)

Hatos said:   8 years ago
I got this output as 11 11.

Am I right?
(2)

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?

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.

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.

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.

Vijay said:   9 years ago
If you say cout executes from right to left then try this.

int y = 10;

cout<<y<< " " <<y++;

Vijay said:   9 years ago
Try this too.

int x = 10;

int &y = x;

cout<< y++ << " " <<y;

Purvi said:   9 years ago
@Vijay.

The output of first is 11 10.
The second is 10 10.
So, what everyone is saying is correct.


Post your comments here:

Your comments will be displayed after verification.