C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Find Output of Program (Q.No. 2)
2.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    union var
    {
        int a, b;
    };
    union var v;
    v.a=10;
    v.b=20;
    printf("%d\n", v.a);
    return 0;
}
10
20
30
0
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
83 comments Page 7 of 9.

Doni said:   1 decade ago
Srivdiya n Risbah answers are good.

RamDas said:   1 decade ago
Sridivya your answer is good

Maddy said:   1 decade ago
Nice expalanation vedavathi.

Nadeem said:   1 decade ago
@rajadurai

Union may have collection of different datatypes. so, it allocates the memory of the largest data type.

Ex:

union xyz{
int x;
float y;
double z;
};

It allocates only 8 bytes for whole. Agree?

Jyoti pandey said:   1 decade ago
Rajadurai is wrong.
Union not allocate for large memory size.
It allocate the last overrided value Whether it is large or small

Karpakam said:   1 decade ago
Good explanation Viswakethu.A

Vandana said:   1 decade ago
At first union stores large value into memory, so we Will get 20 as output.

Ashish said:   1 decade ago
Good answer Dinesh.....

Atul tailwal said:   1 decade ago
I agree to Rishabh

Divya said:   1 decade ago
Good answer Srividhya & Rishabh


Post your comments here:

Your comments will be displayed after verification.