Typedef
File handling

Union

Unoin in C-language

Union in C-language is a special data type available in C that allows to store different data types in the same memory location. You can define a union i n C-language with many members, but only one member can contain a value at any given time. Union in C-language provide an efficient way of using the same memory location for multiple-purpose.

Union in C-languageis also like structure, i.e. collection of different data types which are grouped together. Each element in a union is called member.

Defining a Union

Let’s look at the syntax of a union.

union union_name
{
data-type member-1;
data-type member-2;
data-type member-3;
data-type member-4;
};

As you can see this is same as the syntax of structure, except we use keyword union in place of struct.

Lets try the same example that we took in structue.

union visitor
{
  char name[30];
  int phone_number;
};

Declaration of Union Variable

We declare the variables of a union in the same way as we declare those of a structure.

union visitor
{
  char name[30];
  int phone_number;
};
main()
{
  union vivtor p1, p2;
}

There is another method of declaring union variables

union visitor
{
  char name[30];
  int phone_number;
}p1, p2;

 

Q: What is the difference between Structure and Union?

Difference between structure and union-codingknack

Lets try an example for better understanding.

// C program to illustrate differences 
// between structure and Union
#include<stdio.h>
#include<string.h>
  
// declaring structure
struct struct_example
{
    int integer;
    float decimal;
    char name[20];
};
  
// declaraing union
  
union union_example
{
    int integer;
    float decimal;
    char name[20];
};
  
void main()
{
    // creating variable for structure 
    // and initializing values difference 
    // six
    struct struct_example s={18,38,"geeksforgeeks"};
  
    // creating variable for union 
    // and initializing values
    union union_example u={18,38,"geeksforgeeks"};
  
          
    printf("structure data:\n integer: %d\n"
                "decimal: %.2f\n name: %s\n",
                s.integer, s.decimal, s.name);
    printf("\nunion data:\n integeer: %d\n"
                 "decimal: %.2f\n name: %s\n", 
                u.integer, u.decimal, u.name);
  
  
    // difference two and three
    printf("\nsizeof structure : %d\n", sizeof(s));
    printf("sizeof union : %d\n", sizeof(u));
      
    // difference five
    printf("\n Accessing all members at a time:"); 
    s.integer = 183;
    s.decimal = 90;
    strcpy(s.name, "geeksforgeeks");
      
    printf("structure data:\n integer: %d\n "
                "decimal: %.2f\n name: %s\n",
            s.integer, s.decimal, s.name);
      
    u.integer = 183;
    u.decimal = 90;
    strcpy(u.name, "geeksforgeeks");
      
    printf("\nunion data:\n integeer: %d\n "
                "decimal: %.2f\n name: %s\n",
            u.integer, u.decimal, u.name);
      
    printf("\n Accessing one member at time:");
      
    printf("\nstructure data:");
    s.integer = 240;
    printf("\ninteger: %d", s.integer);
      
    s.decimal = 120;
    printf("\ndecimal: %f", s.decimal);
      
    strcpy(s.name, "C programming");
    printf("\nname: %s\n", s.name);
      
    printf("\n union data:");
    u.integer = 240;
    printf("\ninteger: %d", u.integer);
      
    u.decimal = 120;
    printf("\ndecimal: %f", u.decimal);
      
    strcpy(u.name, "C programming");
    printf("\nname: %s\n", u.name);
      
    //difference four
    printf("\nAltering a member value:\n");
    s.integer = 1218;
    printf("structure data:\n integer: %d\n "
                " decimal: %.2f\n name: %s\n",
                s.integer, s.decimal, s.name);
      
    u.integer = 1218;
    printf("union data:\n integer: %d\n"
           " decimal: %.2f\n name: %s\n",
            u.integer, u.decimal, u.name); 
}
 

structure data:
integer: 18
decimal: 38.00
name: geeksforgeeks

union data:
integeer: 18
decimal: 0.00
name: ?

sizeof structure: 28
sizeof union: 20

Accessing all members at a time: structure data:
integer: 183
decimal: 90.00
name: geeksforgeeks

union data:
integeer: 1801807207
decimal: 277322871721159510000000000.00
name: geeksforgeeks

Accessing one member at a time:
structure data:
integer: 240
decimal: 120.000000
name: C programming

union data:
integer: 240
decimal: 120.000000
name: C programming

Altering a member value:
structure data:
integer: 1218
decimal: 120.00
name: C programming
union data:
integer: 1218
decimal: 0.00
name: ?

Comments are closed.