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 2 of 9.

Keerthana said:   7 years ago
In union, all the variables share the same address space.

Since union can initialize one value at a time,the second assignment statement i.e..,v.b=20 replaces the previous value of v.a=10. So, finally, 20 is stored in union address space which will be shared by both variables a & b.
(2)

Gopi krishna said:   10 years ago
#include<stdio.h>

int main()
{
union var
{
int a, b;
};
union var v;
v.a=10;
v.b=20;
printf("%d %u %u\n", v.a,&v.a,&v.b);
return 0;
}

Output: 20 4291065820 4291065820. Hence only a, b both referring to same address.

Gunjali agarwal said:   1 decade ago
#include<stdio.h>
int main()
{
printf("Weclome to IndiaBIX.com..!");
union var
{
int a;
float b;
};
union var v;
v.a=10;
v.b=20;
printf("%d\n", v.a);
return 0;
}

Output: Garbage value.

Shabana said:   1 decade ago
Because union can initialize only one variable at a time.it will only one members that is greatest the number and last initialized members in aove programe v.a=10;

v.b=20; it will only 20 becoz union can occupy last members that is greatest number.

Nalu said:   1 decade ago
When the data types are different the highest datatype size will be assigned to union and only one variable can be accessed at a time. And it overwrites the same location again and again as long as you use the variables of union. !:).

Monichoron said:   1 decade ago
Union will take only the last value and other will be overwrite such as

main a
{

union v

{
int a,b,c;
}

union v h;
h.a=4;h.b=6;h.c=4;
printf("%d %d",h.a,h.c);

}

It will print only the last value 3

Output will be 3 3

Bips said:   1 decade ago
I think it will give compile error...
cause
v.a=10;
v.b=20;
printf("%d\n", v.a);
since v.b=20 overwrites the value v.a=10
but program is trying to print the value which doesnot exist.
...
..
.
correct me if i m wrong...

Abhishek said:   10 years ago
main a
{

union v

{
int a,b,c;
}

union v h;
h.a=4;h.b=6;h.c=4;
printf("%d %d",h.a,h.c);

}

It will print only the last value 3.

Output will be 3 3.

How the output 3 3 is come? I don't understand about this output.

Srividhya said:   2 decades ago
Because union can initialize only one variable at a time. It overwrites the memory with binary value of 20 where it was initialized with binary value of 10 before.

It takes the last initialized value of its member variables.

Rishabh said:   1 decade ago
Because union can initialize only one variable at a time. It overwrites the memory with binary value of 20 where it was initialized with binary value of 10 before. It takes the last initialized value of its member variables.


Post your comments here:

Your comments will be displayed after verification.