+ 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...

24th May 2021, 3:16 AM
Maniprakash V
Maniprakash V - avatar
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.
24th May 2021, 3:42 AM
A͢J
A͢J - avatar
+ 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.
25th May 2021, 2:30 AM
A͢J
A͢J - avatar
24th May 2021, 3:43 AM
Maniprakash V
Maniprakash V - avatar
+ 1
🅰🅹 🅐🅝🅐🅝🅣 why 20 * 4 bytes? An int is only 4 bytes.
24th May 2021, 9:49 AM
Thoq!
Thoq! - avatar
+ 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.
25th May 2021, 5:12 AM
Thoq!
Thoq! - avatar