Union in c language

Union in c

Union is a user defined data type member in C programming. Which specifically stores different data types program variables elements in the same memory storage location. In union C structure where each structure member has its own individual memory storage location. All variable members of the same union data type use the same memory storage space location. So that while modifying one member of the stored data type elements in union data type, the default values ​​of other existing union members can be affected.

Union in c

Union Data Type Declaration in C.

Just like you have declared the structure member using struct keyword. Similarly in C programming you can declare the union data type members variable using union keyword, just like you have declared the structure data type members in C. Remember for union data type member declaration you have to declare desired union program member along with union keyword.

#include <stdio.h> // standard input output header file

// Define a union named course

union course {

char cname[50];

int duration;

float fee;

};

In the above union program example.

Where a union data type member is declared with a course union name.

Where course union data has three default members. Which includes duration (integer), fee (float), and cname is a 50 character array variable.

Here all the three members of union declared with course name share the same memory space storage location. So if you do any modification in one union member then other union members default storage values ​​may get affected.

Accessing members in C union program.

You can access the declared union members elements in C program by using dot (.) operator like structure, if you have used structure dot operator properly in C program. Union members are also accessed using dot operator in the same way.

int main() {

union course course;

course.duration = 10;

printf(“\n The course.duration is – %d”, course.duration);

course.fee = 799.89;

printf(“\n The course.fee is – %.f”, course.fee);

strcpy(course.cname, “c programming”);

printf(“\n the course.name is – %s”, course.cname);

printf(“\n lets checkout size of union Data is – %lu bytes”, sizeof(course));

return 0;

}

In the above union program example.

Here we have declared a course union data type named union course in C program.

Where we provide unique values ​​to different members of union data type (duration, fee, cname).

Where after setting course.fee and course.cname in manual course union, course.duration union data type member information is displayed. If you modify any one union member, other union members are affected.

Size of union in C.

Size of union data type in C program is determined by the size of its largest union member. Here in the above union program example, cname is an array element of 50 characters. So here the size of union data type is declared as 50 bytes (50 * sizeof(char)) storage.

Uses of union data types in C program.

Union memory management – Use unions in C programs when you need to store different types of program data in a memory-efficient way when only one member is used at a time.

Data type conversion – Unions data types can be used for type conversion, where you deallocate the same bits in memory as different data types.

Different data type handling – Unions data types in C programs can be used to simulate variant records, where a C program can hold and process one of several different types of data at different times in a structure.

Some facts about union data types.

Unions data security – Unions do not provide data type security like structures in C programs. Accessing the wrong data type members declared in a union (e.g., reading course.fee after writing it to course.cname) may provide erroneous behavior or incorrect results.

Union Management – Use union data types in C programs with caution, as they are more difficult to debug and maintain than structure data types when structure members are not used frequently in a C program.

Using unions data types in C programming provides greater flexibility in data representation and memory use. But they need to be operated carefully to ensure data integrity and avoid unintended consequences of sharing data memory between different types.