Unions in C.

Introduction :

Declaring unions :

Declaring union variables :

Accessing union variables :

Initialization a union:

Operations on union :

Following are the list of the operations that are performed on unions.

Union variable can be assigned to another union variable.

Union variable can be passed to a function as a parameter.

Function return a union and a pointer to a union.

The address of the union can be also be obtained by using address of operator(&).

Bit field :

In many cases we declare the variable which holds 20 bytes of memory, e.g., in array concept but actually when we inputted 10 value then rest 10 memory location holds garbage value or zero. To minimize the wastage of memory cells we are going to use bit field. A bit field is a set of adjacent bits, e.g., binary digits(0 to 1). The bit field is used inside the structure members.









THANKS

Union is another way to group data items of different types. Union, like a structure, consists of one or more members which may be of different data types unlike structure in which each member is assigned a unique storage area, in union all the members share the same storage area within the computer's memory. In union, compiler allocates only sufficient storage space for the largest of the members in a union, other members of the union use the value of other members. Thus, union conserve memory.The syntax for declaring a union is similar to syntax of declaring a structure. The only change in the declaration is the substitution of the keyword union in place of the keyword srtuct. Its syntax is,For example,Here,is a keyword that specifies the union data typeis the tag name of the union. It is used by the compiler to identify the union. The variablesandare the members of the union and are declared within the curly braces. The union declaration ends with a semicolon.Just like structures, anonymous union also exist. Anonymous union is a union without name. Union variable of anonymous union is declared at the end of union declaration. For example, the statement declares a anonymous union.After declaring the union , you need to declare union variables. Union variables are declared in the same way as they are declared for structures. for example, the statement,union item t;declares a variable t of union item type. The compiler reserves enough storage area to store the largest member of the union. In our example, the size of the largest member (i.e. b) is equal to 4 (float type requires 4 bytes) so the size of union variable t will be 4. Had it been a structure then a total of bytes would have been required to store a structure variable (as sum of bytes required by all three variables is 1+2+4=7 bytes),Defining union variable is as follows,orUnion members can be referred in the same manner as structure members. We can refer union members either using a dotoperator or arrowoperator. However, unlike structure in which all the structure members can be accessed at any point of time, in union only one of the member can be accessed at any given time . This means that the member which is last written has a meaningful value and all other contain garbage values.The statement,accesses themember of the union variable atAlthough union members are referred in the same manner as that of structure members but all the members cannot be initialized together. This is because memory location of union is shared by different members of the union at different times. The size of union is equal to the size of the largest data members. So it would not be possible to initialize all members of a union together as value of latest initialized member overwrites the value of the preceding members. You can only initialize the first member declared in the union when the union variable is declared. The other members can only be initialized by assigning values or reading value into the union. Keep in mind that while initializing the union, it must enclose a value within a set of braces.Program to show how to initialize the members of the union.The output reveals that the latest initialized member of the union overwrites the value of the preceding member.One can nest a union within a structure. This is helpful when a structure member is allowed to hold one of the union member.