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 2 of 9.
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.
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.
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
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
Mitha mazumdar said:
1 decade ago
But 'c' cannot create objects as java does so how it is possible?
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.
Smitha said:
1 decade ago
#include<stdio.h>
int main()
{
union var
{
int a, b, c;
};
union var v;
v.a=10;
v.b=20;
printf("%d\n", v.c);
return 0;
}
Even if we add a third variable 'c' to the union and try to print it would output the same value =20 since it will always output the last value in such cases.
int main()
{
union var
{
int a, b, c;
};
union var v;
v.a=10;
v.b=20;
printf("%d\n", v.c);
return 0;
}
Even if we add a third variable 'c' to the union and try to print it would output the same value =20 since it will always output the last value in such cases.
Siraj Ansari said:
1 decade ago
There are two concept first one is union by default preferred higher memory allocation, and second one is union has print single value and that value should be higher 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.
v.b=20; it will only 20 becoz union can occupy last members that is greatest number.
Janardhan reddy said:
1 decade ago
In C compiler allocates 2 bytes of memory to union and int needs 2 bytes. In this way first value is deleted and only 2nd value is present, in place of first value. So output is 20.
Rashed said:
10 years ago
I didn't understand how it becomes 20 it must be 10.
Remember me!! said:
1 decade ago
Here you can see the difference:
You have to print value of 'a' before 'b' get declared to 20. Because union always display the datatype having larger memory.
#include<stdio.h>
int main()
{
union var
{
int a, b;
};
union var v;
v.a=10;
printf("%d\n", v.a);
v.b=20;
getch();
return 0;
}
The reason why union is used means it hold very less memory than structure it doesn't waste memory. I believe its a good think.
You have to print value of 'a' before 'b' get declared to 20. Because union always display the datatype having larger memory.
#include<stdio.h>
int main()
{
union var
{
int a, b;
};
union var v;
v.a=10;
printf("%d\n", v.a);
v.b=20;
getch();
return 0;
}
The reason why union is used means it hold very less memory than structure it doesn't waste memory. I believe its a good think.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers