C++ Programming - References - Discussion

Discussion Forum : References - Programs (Q.No. 14)
14.
What will be the output of the following program?
#include <iostream.h> 
enum xyz 
{
    a, b, c
}; 
int main()
{
    int x = a, y = b, z = c; 
    int &p = x, &q = y, &r = z; 
    p = z; 
    p = ++q;
    q = ++p;
    z = ++q + p++; 
    cout<< p << " " << q << " " << z;
    return 0; 
}
2 3 6
4 4 7
4 5 8
3 4 6
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
10 comments Page 1 of 1.

Rajendra mahanor said:   1 decade ago
enum started value a=0, b=1, c=3 then,

Assign value x=a=0, y=b=1, z=c=2 then,

Reference value,

&p = x = 0.
&q = y = 1.
&r = z = 2.

Now p=z means p=2 again p=++q pre-increment &q=y=1, p=++q=2.
Then,

q = ++p = 3.
After q=4.

z = ++q+p++.
z = 3+4.
z = 7.
p = 4.

So output is p=4, q=4 and z=7.
(1)

Amarnath said:   1 decade ago
We can't change(++, --) enum values.
(1)

Sandesh said:   5 years ago
How q==4? Please anyone explain it.
(1)

Bidyut said:   8 years ago
How a, b, c are given values?

Pritha said:   8 years ago
How can we assign the values of a, b, c in such a random manner?

Abhinav said:   8 years ago
Explain it.

Manimegalai said:   7 years ago
Please, anybody explain it.

Vishal singh said:   7 years ago
It's the property of enum that they are initialized from 0 and stay in sequential increment order (i.e. A=0, b=1, c=2), also by enum property we can't change the value of enum after initialization.

Tavish said:   6 years ago
How can we random assigned the values to a b and c.

Minu said:   6 years ago
P=z=2.
P=++q=++1=2.
Q=++p=++2=3.
Z=++q+p++
Z=4+3=7.
Q=4.
P=3++=4.

Post your comments here:

Your comments will be displayed after verification.