+ 1
#include<stdio.h> union terca{ int a; int b; }; int main() { union terca v; v.a=10; v.a=20; printf ("%d",v.a);
About union concept... Please explain me how the output is 20...
6 Antworten
+ 7
It's a Union. It is a collection of variables of different data types in the same memory location. The memory occupied by a union will be large enough to hold the largest member of the union.
Data type will occupy 20 * 4 bytes of memory space because this is the maximum space which can be occupied by an int. That is why output is 20.
+ 2
Thoq!
Yes int has 4 bytes but there is v.a = 20 so total bytes will be 20 * 4 which is max in given union so memory will occupy max bytes.
+ 1
Thank you 🅰🅹 🅐🅝🅐🅝🅣 bro
+ 1
🅰🅹 🅐🅝🅐🅝🅣 why 20 * 4 bytes? An int is only 4 bytes.
+ 1
🅰🅹 🅐🅝🅐🅝🅣
That would be the case if v.a were an array. Since it isn't, the size of the union is only 4 bytes.