Union in C
Union is a
collection of variables of different data types. Union is useful for representing
logically related information of different data types.
In union all members share one common memory space. Compiler allocates memory space such that it can accommodate largest member of the union. Syntax
union union_name { datatype variable1; datatype variable2; .................. datatype variableN; }; Union can
be declared using union keyword. Example
union Item { int qty; float price; }; Here, Item is a union having two members declared inside it. Variable of
union can be declared using union keyword and union name. Syntax
In above example
union Item having two members.union union_name Var1,Var2,Var3; Example union Item I1, I2, I3;
Here largest memory occupied is 4 bytes so 4 bytes allocated to each union variable
I1, I2 and I3. Syntax
union_variable_name.member_name; Example I1.qty=5; I1.price=50.50; |
||||||||||||||||||