Basic Unions in C Programming | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

union 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 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-purpose.

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 tag] {
member definition;
member definition;
...
member definition;
} [one or more union variables];

The union tag is optional and each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. At the end of the union's definition, before the final semicolon, you can specify one or more union variables but it is optional. Here is the way you would define a union type named Data having three members i, f, and str −

union Data {
int i;
float f;
char str[20];
} data;

Now, a variable of Data type can store an integer, a floating-point number, or a string of characters. It means a single variable, i.e., same memory location, can be used to store multiple types of data. You can use any built-in or user defined data types inside a union based on your requirement.

The memory occupied by a union will be large enough to hold the largest member of the union. For example, in the above example, Data type will occupy 20 bytes of memory space because this is the maximum space which can be occupied by a character string. The following example displays the total memory size occupied by the above union −

#include <stdio.h>
#include <string.h>

union Data {
int i;
float f;
char str[20];
};

int main( ) {

union Data data;

printf( "Memory size occupied by data : %d
", sizeof(data));

return 0;
}

When the above code is compiled and executed, it produces the following result −

Memory size occupied by data : 20

Accessing Union Members

To access any member of a union, we use the member access operator (.). The member access operator is coded as a period between the union variable name and the union member that we wish to access. You would use the keyword union to define variables of union type. The following example shows how to use unions in a program −

#include <stdio.h>
#include <string.h>

union Data {
int i;
float f;
char str[20];
};

int main( ) {

union Data data;

data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");

printf( "data.i : %d
", data.i);
printf( "data.f : %f
", data.f);
printf( "data.str : %s
", data.str);

return 0;
}

When the above code is compiled and executed, it produces the following result −

data.i : 1917853763
data.f : 4122360580327794860452759994368.000000
data.str : C Programming

Here, we can see that the values of i and f members of union got corrupted because the final value assigned to the variable has occupied the memory location and this is the reason that the value of str member is getting printed very well.

Now let's look into the same example once again where we will use one variable at a time which is the main purpose of having unions −

#include <stdio.h>
#include <string.h>

union Data {
int i;
float f;
char str[20];
};

int main( ) {

union Data data;

data.i = 10;
printf( "data.i : %d
", data.i);

data.f = 220.5;
printf( "data.f : %f
", data.f);

strcpy( data.str, "C Programming");
printf( "data.str : %s
", data.str);

return 0;
}

When the above code is compiled and executed, it produces the following result −

data.i : 10
data.f : 220.500000
data.str : C Programming

Here, all the members are getting printed very well because one member is being used at a time.

The document Basic Unions in C Programming | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Basic Unions in C Programming - Programming and Data Structures - Computer Science Engineering (CSE)

1. What is a union in C programming?
Ans. A union in C programming is a user-defined data type that allows storing different data types in the same memory location. It helps in saving memory when only one member is used at a time. The size of a union is determined by the size of its largest member.
2. How do you declare a union in C?
Ans. To declare a union in C, the keyword "union" is used followed by the union name and a list of members inside curly braces. Each member within the union can have a different data type. For example: ``` union SampleUnion { int num; float decimal; char character; }; ```
3. What is the purpose of using unions in C programming?
Ans. The purpose of using unions in C programming is to save memory by sharing the same memory location for storing different types of data. It allows efficient memory utilization when only one member of the union is accessed at a time. Unions are commonly used in scenarios where a single variable needs to hold different types of data at different times.
4. How do you access members of a union in C?
Ans. In C, the members of a union can be accessed using the dot (.) operator. However, it is important to note that only one member of the union can be accessed at a time. Accessing a different member of the union will overwrite the value of the previously accessed member. For example: ``` union SampleUnion { int num; float decimal; char character; }; int main() { union SampleUnion sample; sample.num = 10; printf("Value of num: %d\n", sample.num); sample.decimal = 3.14; printf("Value of decimal: %f\n", sample.decimal); sample.character = 'A'; printf("Value of character: %c\n", sample.character); return 0; } ```
5. Can a union contain an array as one of its members in C?
Ans. Yes, a union in C can contain an array as one of its members. The size of the array will determine the overall size of the union. However, it is important to note that when accessing the array member, the entire array will be treated as a single unit. Accessing individual elements of the array within the union may lead to unexpected results.
119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

video lectures

,

Viva Questions

,

Basic Unions in C Programming | Programming and Data Structures - Computer Science Engineering (CSE)

,

ppt

,

MCQs

,

Basic Unions in C Programming | Programming and Data Structures - Computer Science Engineering (CSE)

,

Summary

,

Semester Notes

,

Important questions

,

mock tests for examination

,

study material

,

Free

,

practice quizzes

,

Objective type Questions

,

Extra Questions

,

pdf

,

Previous Year Questions with Solutions

,

Exam

,

Sample Paper

,

shortcuts and tricks

,

past year papers

,

Basic Unions in C Programming | Programming and Data Structures - Computer Science Engineering (CSE)

;