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;
}
Discussion:
83 comments Page 8 of 9.
Palaniarjun said:
9 years ago
Hi, friends
#include<stdio.h>
int main()
{
union var
{
int a, b;
};
union var v;
v.a=100;
v.b=10;
printf("%d\n", v.a);
return 0;
}
---------------------------------------
Out put =10
------------------------------------------
Union allocates highest memory location for the data type(int,float,char) in these data type float has the highest memory so union allocate 4 bytes. And union store only one value it override the previous value,so it produces the last declaration or initialization value.
#include<stdio.h>
int main()
{
union var
{
int a, b;
};
union var v;
v.a=100;
v.b=10;
printf("%d\n", v.a);
return 0;
}
---------------------------------------
Out put =10
------------------------------------------
Union allocates highest memory location for the data type(int,float,char) in these data type float has the highest memory so union allocate 4 bytes. And union store only one value it override the previous value,so it produces the last declaration or initialization value.
Abhijeet Apar said:
9 years ago
I agree, with your answer but in the previous question, we got two values and both are also of same variable type. Then why this condition is not satisfied in that question? Please clear my doubt.
Reshma said:
9 years ago
Union allocates memory for the large memory size.
Vignesh said:
8 years ago
Thanks for all the explanation. Thanks for all your help, I understand it now.
Mads said:
8 years ago
Why the output is garbage value for the following program? Can anyone explain?
int main()
{
union var
{
int a;
float b;
};
union var v;
v.a=10;
v.b=20.4;
printf("%d\n", v.a);
return 0;
}
int main()
{
union var
{
int a;
float b;
};
union var v;
v.a=10;
v.b=20.4;
printf("%d\n", v.a);
return 0;
}
Kps lubana said:
8 years ago
0 is the Answer.
(1)
Venkat g said:
7 years ago
Thanks @KPS Lubana.
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.
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)
Mounisha said:
7 years ago
The union will allocate for all datatypes in the same memory location, the only structure will allocate the separate memory.
The location for each data types. So, it can initialize only one value at a time v.b=20 has replaced the value of v.a=10, So it will be stored in address space 20.
The location for each data types. So, it can initialize only one value at a time v.b=20 has replaced the value of v.a=10, So it will be stored in address space 20.
(3)
Karan said:
6 years ago
How it will print 20? I am not getting, please anyone tell me.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers