The perfect place for easy learning...

C Programming Language

×

Topics List

Place your ad here

Unions in C

In C programming language, the union is a collection of elements of the different data type. The union is used to create user-defined data type in the C programming language. As the union used to create a user-defined data type, the union is also said to be “user-defined data type in C”.
In other words, the union is a collection of non-homogeneous elements. Using union we can define new data types called user-defined data types that holds multiple values of the different data type. The formal definition of a union is as follows...

Union is a colloction of different type of elements under a single name that acts as user defined data type in C.

Generally, unions are used to define a record in the c programming language. Unions allow us to combine elements of a different data type into a group. The elements that are defined in a union are called members of union.

How to create union?

To create union in c, we use the keyword called "union". We use the following syntax to create unions in c programming language.

union <structure_name>
{
      data_type member1;
      data_type member2, member3;
      .
      .
} ;

Following is the example of creating a union called Student which is used to hold student record.

Creating union in C

union Student
{
    char stud_name[30];
    int roll_number;
    float percentage;
} ;


Importent Points to be Remembered
  Every union must terminated with semicolon symbol (;).
  "union" is a keyword, it must be used in lowercase letters only.


Creating and Using union variables

In a c programming language, there are two ways to create union variables. We can create union variable while the union is defined and we can also create after terminating union using union keyword.
TO access members of a union using union variable, we use dot (.) operator. Consider the following example code...

Creating and Using union variables in C

union Student
{
    char stud_name[30];
    int roll_number;
    float percentage;
} stud_1 ;                  // while defining union

void main(){
    union Student stud_2;  // using union keyword
    
    printf("Enter details of stud_1 : \n");
    printf("Name : ");
    scanf("%s", stud_1.stud_name);
    printf("Roll Number : ");
    scanf("%d", &stud_1.roll_number);
    printf("Percentage : ");
    scanf("%f", &stud_1.percentage);
    
    printf("***** Student 1 Details *****\n);
    printf("Name of the Student : %s\n", stud_1.stud_name);
    printf("Roll Number of the Student : %i\n", stud_1.roll_number);
    printf("Percentage of the Student : %f\n", stud_1.percentage);
}

In the above example program, the union variable "stud_1 is created while defining the union and the variable "stud_2 is careted using union keyword. Whenever we access the members of a union we use the dot (.) operator.

Memory allocation of Union

When the unions are used in the c programming language, the memory does not allocate on defining union. The memory is allocated when we create the variable of a particular union. As long as the variable of a union is created no memory is allocated. The size of memory allocated is equal to the maximum memory required by an individual member among all members of that union. In the above example program, the variables stud_1 and stud_2 are allocated with 30 bytes of memory each.



Place your ad here
Place your ad here