Difference Between Structure And Union in C

Difference Between Structure And Union in C

  • Structures in C: Structures are user-defined data types in C that enable the combination of data items of different types into a single entity. They are often used to represent records or complex data structures.
  • Defining a Structure: To define a structure, you use the struct statement. This statement creates a new data type with one or more members, each of which can have its own data type. Structures allow you to encapsulate related data items within a single unit, providing organization and clarity to your data.
 union [union name]
    {
       member definition;
       member definition;
       ...
       member definition;
    };
   
    (OR) 

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

Similarities Between Structure and Union

The shared and distinguishing features are given below:

  • Shared Characteristics:
  1. Both are user-defined data types.
  2. They store data of different types within a single unit.
  3. Members can be objects of any type, including other structures, unions, or arrays.
  4. Members can include bit fields for efficient memory usage.
  5. Both support the = (assignment) and sizeof operators.
  6. They require structures or unions in assignment to have the same members and member types.
  7. They can be passed by value to functions and returned by value by functions, provided the argument and function parameter have the same type.
  8. The ‘.’ (dot) operator is used to access member variables in both structures and unions.

These commonalities make structures and unions versatile for storing and organizing data. However, it’s essential to recognize that unions differ in how they allocate memory for members compared to structures. While structures allocate separate memory for each member, unions share the same memory location among members, allowing only one member to be active at any given time.

Difference between Structure and Union

Structure Union
KeywordThe keyword struct functions to define a structure.The union keyword functions to define union
Size When variable is associated with structure,the compiler will allocate with the memory of each memory. The size of structure will be greater than or equal to the sum of its membersWhen Variable is assosiated with union, the compiler will allocate the memory by checking the size of largest memeory.Hence, the size of union will be equal to size of largest member.
MemoryEach member within a structure will be assigned unique storage area of location.Memory allocated will be shared by the individual members of union.
Value AltAltering the value of member cannot affect other members of the structure.Altering the value of any of the member will automatically alter other member values.
Accessing membersIndividual member will be accessed at any time.Whereas, in union , we can see that only one member will be accessed.
Initializing of membersSeveral member of a structure will be initialized at once. Only the first member of union will be initialized.

Example


// 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]; 
}; 
  
// declaring 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 integer: %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 integer: %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); 
}

Output

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

union data:
 integer: 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:
 integer: 1801807207
 decimal: 277322871721159507258114048.00
 name: geeksforgeeks

 Accessing one member at 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: �

Time Complexity: O(1)

Auxiliary Space: O(1)

FAQ- Difference Between Structure and Union in C

Q1. Why union is better than structure in C?

Ans. In a union, only one element is used at a time because they share the same memory location. This is handy for storing data that can be of various types. In a struct, each element has its own memory space, and all can be used simultaneously. Structures are suitable for data with multiple elements of different types.

Q2. Which is greater size of struct and union?

Ans. Structure Size: A structure’s size is the sum of its data members’ sizes and any required padding for alignment.
Union Size: The size of a union is equal to the size of its largest data member, and only the most recently initialized member holds a value.

Q3. What is the difference between structure and union which is faster?

Ans. Your explanation succinctly highlights the key points about unions:
Memory Efficiency: Unions consume less memory compared to structures because they allocate space only for their largest data member.
Direct Access to the Largest Member: When using a union, you can directly access the memory allocated for the largest data member.
Efficient Memory Usage: Unions are ideal when you want to conserve memory and use the same memory location for different data members, depending on the active member.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment