Tutorialspoint.dev

Difference between Structure and Union in C

structures in C

A structure is a user-defined data type available in C that allows to combining data items of different kinds. Structures are used to represent a record.
Defining a structure: To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member. The format of the struct statement is as follows:

   struct [structure name]
   {
       member definition;
       member definition;
       ...
       member definition;
   };
  

union

A union is a special data type available in C that allows storing different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple purposes.
Defining a Union: To define a union, you must use the union statement in the same way as you did while defining a structure. The union statement defines a new data type with more than one member for your program. The format of the union statement is as follows:



    union [union name]
    {
       member definition;
       member definition;
       ...
       member definition;
    };

Similarities between Structure and Union

  1. Both are user-defined data types used to store data of different types as a single unit.
  2. Their members can be objects of any type, including other structures and unions or arrays. A member can also consist of a bit field.
  3. Both structures and unions support only assignment = and sizeof operators. The two structures or unions in the assignment must have the same members and member types.
  4. A structure or a union can be passed by value to functions and returned by value by functions. The argument must have the same type as the function parameter. A structure or union is passed by value just like a scalar variable as a corresponding parameter.
  5. ‘.’ operator is used for accessing members.

Differences

// 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: integer: %d " 
                "decimal: %.2f name: %s ",
                s.integer, s.decimal, s.name);
    printf(" union data: integeer: %d "
                 "decimal: %.2f name: %s "
                u.integer, u.decimal, u.name);
   
   
    // difference two and three
    printf(" sizeof structure : %d ", sizeof(s));
    printf("sizeof union : %d ", sizeof(u));
       
    // difference five
    printf(" Accessing all members at a time:"); 
    s.integer = 183;
    s.decimal = 90;
    strcpy(s.name, "geeksforgeeks");
       
    printf("structure data: integer: %d "
                "decimal: %.2f name: %s ",
            s.integer, s.decimal, s.name);
       
    u.integer = 183;
    u.decimal = 90;
    strcpy(u.name, "geeksforgeeks");
       
    printf(" union data: integeer: %d "
                "decimal: %.2f name: %s ",
            u.integer, u.decimal, u.name);
       
    printf(" Accessing one member at time:");
       
    printf(" structure data:");
    s.integer = 240;
    printf(" integer: %d", s.integer);
       
    s.decimal = 120;
    printf(" decimal: %f", s.decimal);
       
    strcpy(s.name, "C programming");
    printf(" name: %s ", s.name);
       
    printf(" union data:");
    u.integer = 240;
    printf(" integer: %d", u.integer);
       
    u.decimal = 120;
    printf(" decimal: %f", u.decimal);
       
    strcpy(u.name, "C programming");
    printf(" name: %s ", u.name);
       
    //difference four
    printf(" Altering a member value: ");
    s.integer = 1218;
    printf("structure data: integer: %d "
                " decimal: %.2f name: %s ",
                s.integer, s.decimal, s.name);
       
    u.integer = 1218;
    printf("union data: integer: %d "
           " decimal: %.2f name: %s ",
            u.integer, u.decimal, u.name); 
}

Output:

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: ?

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter