C Bit Field

Advertisements

Union in C

Both structure and union are collection of different datatype. They are used to group number of variables of different type in a single unit.

Difference betweenw Structure and union.

1. Declaration and Initialization of structure starts with struct keyword. Declaration and Initialization of union starts with union keyword.

2. Structure allocates different memory locations for all its members while union allocates common memory location for all its members. The memory occupied by a union will be large enough to hold the largest member of the union.

Union declaration

Declaration of union must start with the keyword union followed by the union name and union's member variables are declared within braces.

Syntax for declaring union


              union union-name
              {
                     datatype var1;
                     datatype var2;
                     - - - - - - - - - -
                     - - - - - - - - - -
                     datatype varN;
              };

Accessing the union members

We have to create an object of union to access its members. Object is a variable of type union. Union members are accessed using the dot operator(.) between union's object and union's member name.

Syntax for creating object of union


              union union-name obj;

Example for creating object & accessing union members


       #include<stdio.h>

       union Employee
       {
              int Id;
              char Name[25];
              int Age;
              long Salary;
       };

       void main()
       {

              union Employee E;

                    printf("\nEnter Employee Id : ");
                    scanf("%d",&E.Id);

                    printf("\nEnter Employee Name : ");
                    scanf("%s",&E.Name);

                    printf("\nEnter Employee Age : ");
                    scanf("%d",&E.Age);

                    printf("\nEnter Employee Salary : ");
                    scanf("%ld",&E.Salary);

                    printf("\n\nEmployee Id : %d",E.Id);
                    printf("\nEmployee Name : %s",E.Name);
                    printf("\nEmployee Age : %d",E.Age);
                    printf("\nEmployee Salary : %ld",E.Salary);


       }

   Output :

              Enter Employee Id : 1
              Enter Employee Name : Kumar
              Enter Employee Age : 29
              Enter Employee Salary : 45000

              Employee Id : -20536
              Employee Name : ?$?$  ?
              Employee Age : -20536
              Employee Salary : 45000

In the above example, we can see that values of Id, Name and Age members of union got corrupted because final value assigned to the variable has occupied the memory location and this is the reason that the value of salary member is getting printed very well. Now let's look into the same example once again where we will use one variable at a time which is the main purpose of having union:


       #include<stdio.h>

       union Employee
       {
              int Id;
              char Name[25];
              int Age;
              long Salary;
       };

       void main()
       {

              union Employee E;

                    printf("\nEnter Employee Id : ");
                    scanf("%d",&E.Id);
                    printf("Employee Id : %d",E.Id);

                    printf("\n\nEnter Employee Name : ");
                    scanf("%s",&E.Name);
                    printf("Employee Name : %s",E.Name);

                    printf("\n\nEnter Employee Age : ");
                    scanf("%d",&E.Age);
                    printf("Employee Age : %d",E.Age);

                    printf("\n\nEnter Employee Salary : ");
                    scanf("%ld",&E.Salary);
                    printf("Employee Salary : %ld",E.Salary);

       }

   Output :

              Enter Employee Id : 1
              Employee Id : 1

              Enter Employee Name : Kumar
              Employee Name : Kumar

              Enter Employee Age : 29
              Employee Age : 29

              Enter Employee Salary : 45000
              Employee Salary : 45000

Here, all the members are getting printed very well because one member is being used at a time.

Example of comparing size of union and structure


	#include<stdio.h>

	  struct Employee1
	  {
			 int Id;
			 char Name[25];
			 long Salary;
	  };

	  union Employee2
	  {
			 int Id;
			 char Name[25];
			 long Salary;
	  };

	   void main()
	   {

			printf("\nSize of Employee1 is : %d",sizeof(Employee1));
			printf("\nSize of Employee2 is : %d",sizeof(Employee2));

	   }

	   Output :

			  Size of Employee1 is : 31
			  Size of Employee2 is : 25

Related topics

Advertisement