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 3 of 9.
Vedavathi said:
1 decade ago
In union all the members should be different data types. If two variables having the same datatype should be overwrited in memory. Here both are int values only, so first 10 is overwrited by 20. So 20 will be displayed.
Pradeep said:
1 decade ago
In unions, we are getting data from particular allocated memory for only one variable, in doesnt matter about datatype. We can awaken only one variable in union at a time and getting overriden data. That is "b" value.
Alex said:
1 decade ago
As the principle of Union- All the variable shares the same memory locations and at time only one value can be accessed so memory allocated by the highest demand of variable so that other variable also can use that.
Alfaz raza khan said:
1 decade ago
All the members of union are stored at the same address, this means there is only one common memory. Hence in this case initially,
v.a = 10;//10 will be stored.
v.b = 20;//at the same address 20 will be stored.
v.a = 10;//10 will be stored.
v.b = 20;//at the same address 20 will be stored.
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?
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?
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.
Kalai said:
2 years ago
Union: a collection of different data items stored in a same memory location.
-> last store data is the first priority. So 20 is printed because all data is stored in the same memory location.
-> last store data is the first priority. So 20 is printed because all data is stored in the same memory location.
(8)
Atul kumar said:
1 decade ago
Union allocates single memory for all variables, which has the highest size. So first value(10) will be overwrite with second value(20) because memory location is one. Hence output will be 20.
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;
}
Rahul said:
1 decade ago
Above program memory for int allocate once in union, first it store 'a' variable in memory value, later it OVERRIDE with RECENT initialize 'b'.
Value, so the output will be 'b' value.
Value, so the output will be 'b' value.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers