Difference between Union and Structure in C

Difference between Union and Structure in C

Difference between union and structures in c programming; Through this tutorial, you will know what is the difference between union and structures in c programming with the help of examples.

In the C programming language many built-in data types. And also It allows creating your own data types. There are five ways for creating custom data, namely bit-field, structure, union, typedef, and enumeration. And it is also known as user-defined data type.

Note that:- The structure and union are user-defined data types in C programming that can hold multiple data types. While both structure and union seem to perform similar functions, there are several differences between them.

Difference between Union and Structure in C

Let’s understand the difference between structure and union in c programming with the help of syntax, declaration and examples:

  • Definition of Structure
  • How to Define a Structure?
  • Definition of Union
  • How to Define a Union?
  • Similarities between Structure and Union
  • Difference between Structure and Union

Definition of Structure

A structure is a custom data type in the C language. Structures can hold multiple members of different data types under a single unit. The elements of a structure are stored in contiguous memory locations and can be retrieved and accessed at any time. Every data object in a structure is a member or field.

How to Define a Structure?

Using the struct keyword, you can define structure in c programming. As shown below:

Syntax of Declaring a Structure

struct [structure name]{type member_1;type member_2;. . .type member_n;};

Note that:- The struct keyword defines a new data type with more than one member.

Example of Structure

#include <stdio.h>
struct student {
    char name[60];
    int roll_no;
    float marks;
} sdt;
int main() {
    printf("Enter the following information:\n");
    printf("Enter student name: ");
    fgets(sdt.name, sizeof(sdt.name), stdin);
    printf("Enter student roll number: ");
    scanf("%d", & sdt. roll_no);
    printf("Enter students marks: ");
    scanf("%f", & sdt.marks);
    printf("The information you have entered is: \n");
    printf("Student name: ");
    printf("%s", sdt.name);
    printf("Student roll number: %d\n", sdt. roll_no);
    printf("Student marks: %.1f\n", sdt.marks);
    return 0;
}

Definition of Union

A Union is a user-defined data type. It is similar to structure except for one thing about location in memory. Union in c programming is a collection of variables of different datatypes in the shared memory location.

Note that:- Unions and structures are conceptually similar. The basic difference is in terms of storage. In structure each member or variable has its own storage location, whereas all members of union uses a single shared memory location which is equal to the size of its largest data member or variable.

How to Define a Union?

Using the union keyword, you can define union in c programming. As shown below:

Syntax of Declaring a Union

union [union name]{type member_1;type member_2;. . .type member_n;}; 

Note that:- It defines a new data type that can store multiple member variables of different data types in the same memory location.

Example of Union

#include <stdio.h>

union item
{
    int x;
    float y;
    char ch;
};

int main( )
{
    union item it;
    it.x = 12;
    it.y = 20.2;
    it.ch = 'a';
    
    printf("%d\n", it.x);
    printf("%f\n", it.y);
    printf("%c\n", it.ch);
    
    return 0;
}

Similarities between Structure and Union

The following are the similarities between structure and union:

  • Both structure and union are the custom data types that store different types of data together as a single entity
  • The members of structure and union can be objects of any type, such as other structures, unions, or arrays.
  • Both union or a structure can be passed by value to a function and also return to the value by functions. The argument will need to have the same type as the function parameter.
  • To access members, we use the ‘.’ operator.

Difference between Structure and Union

Both a structure and a union combine different information related to a given entity. The main difference between the two is how specific information is stored and accessed. The table below highlights the key differences between structure and union:

Let’s summarize the above discussed topic about the Struct and Union in the form of a table that highlight the differences between structure and union:

StructUnion
The struct keyword is used to define a structure.The union keyword is used to define union.
When the variables are declared in a structure, the compiler allocates memory to each variables member. The size of a structure is equal or greater to the sum of the sizes of each data member.When the variable is declared in the union, the compiler allocates memory to the largest size variable member. The size of a union is equal to the size of its largest data member size.
Each variable member occupied a unique memory space.Variables members share the memory space of the largest size variable.

Here, Memory allocated is the sum of sizes of all datatypes in structure(Here, 7bytes)

Here, Memory allocated is the maximum size allocated among all the datatypes in union(Here, 4bytes)
Changing the value of a member will not affect other variables members.Changing the value of one member will also affect other variables members.
Each variable member will be assessed at a time.Only one variable member will be assessed at a time.
We can initialize multiple variables of a structure at a time.In union, only the first data member can be initialized.
All variable members store some value at any point in the program.Exactly only one data member stores a value at any particular instance in the program.
The structure allows initializing multiple variable members at once.Union allows initializing only one variable member at once.
It is used to store different data type values.It is used for storing one at a time from different data type values.
It allows accessing and retrieving any data member at a time.It allows accessing and retrieving any one data member at a time.

Conclusion

Difference between union and structures in c; Through this tutorial, we have learned what is the difference between union and structures in c programming with the help of examples.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *