C++ Programming - References - Discussion

Discussion Forum : References - Programs (Q.No. 5)
5.
Which of the following statement is correct about the program given below?
#include<iostream.h> 
enum bix
{
    a=1, b, c
};
int main()
{
    int x = c;
    int &y = x;
    int &z = x;
    y = b;
    cout<< z--;
    return 0; 
}
It will result in a compile time error.
The program will print the output 1.
The program will print the output 2.
The program will print the output 3.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
15 comments Page 1 of 2.

Raj said:   7 years ago
enum bix
{
a=1,b,c
// this means : a=1, b=2,c=3//.
};
int main()
{
int x=c; //means x=3;
int &y=x;
int &z=x;
// this means is x=y=z=3;
y=b;
//in this we assign y=2;
// means x=y=z=2 Right now
cout<<z--; //post increment,
}

Hence output is 2.

Thank you.

Tushar S. said:   8 years ago
x=y=z=2;

y=b;//assign AND z--; means post decrements.

cout<< z-- ;//First display the outpunt z=2 AND after post decrements by z=z-1 or z--;
// after after post decrements value of x,y,z is x=y=z=1;

Abhishek said:   1 decade ago
Since we have use enum and we have declared 3 variables inside it.

1st one is initialized to 1, by enum property b=2, c=3.

All variables get increment by one thereafter.

For next part of answer see answer of D. S. Raghuram.

Sukanya said:   6 years ago
Enum used a=1, so next variables automatic takes next values,b=2 and c=3.
x=y=z=3;
After y=b assigned so x=y=z=2;
The output will be z-- ,it is a post decrement.
So the output is z=2.
(6)

D.s.raghuram said:   1 decade ago
In enum here a=1,hence then b will be 2 and c will be 3.
x=3.
y=3.
z=3.

y=2 i.e.
x=2 i.e.
z=2.

And decrement operator will effect only after the output !

Thus output = 2.

Puru said:   1 decade ago
I don't understand how the reference variable's value is allowed to change. I read that a reference variable can't change its reference.

Could anyone explain?

Mohammad Mohsin said:   10 years ago
Here, y = b then all x, y and z will point to b and b = 2 as enum used.

And z-- makes no change because post decrement operator is used.

Prerak said:   1 decade ago
x=3
y=3
z=3

y=2 i.e.
x=2 i.e.
z=2

And decrement operator will effect only after the output !

Thus output = 2.

But z = 1.

Honey Garg said:   1 decade ago
Because here we used enum. In enum when we initialized variable the value will be in incremented order.

Vikas said:   10 years ago
z worked here y=b does not affect previous and z=3 remains still then after decrements it become z=2.


Post your comments here:

Your comments will be displayed after verification.