Union In C Language – Syntax, Initialization, Example Program of Union In C
Just as we create structure in C language, in the same way, we are going to make union here, so if you do not know about structure well, then first of all you should know about structure from here -:
Everything we will study in this article is primarily related to the concept of structure.
Since many concepts of structure are similar to union, a comparison is also made between structure and union.
In this article, we will learn in detail about What is Union In C? And How to use union in C language? Along with you will also know what is the difference between structure and union?
So without wasting time let’s first understand what is Union In C Language?
What is Union In C Language?
Union is also a user-defined data type, like structure that contains data members of different data types.
All the members present in the union share the same location, whereas in the structure each member got separate memory.
In union, more than one member cannot get memory at the same time, so only one member can contain a value at a time, whereas in structure all the members can contain different values at the same time.
Syntax of Declaring Union In C
To create a union in C language, we have to use the union keyword, its basic syntax is -:
Syntax -:
union union_name
{
//member definitions
Data_Type member1_variable;
Data_Type member2_variable;
Data_Type member3_variable;
…
};
- union is a keyword with the help of which we create new data type.
- union_name – This is the name of the union data type, you can keep this name as per your choice like employee, college, item, etc.
- DataType – DataType can contain int, char, float, double anything.
- member1_variable – This is the name of the variable. Every element located inside the union is called a member.
Example -:
union item
{ int x;
float y;
char z;
};
Note -: The size of the union is equal to the size of the largest data member of the union. For example – If a union has a union member of 1, 2, 4, 8 bytes, then the size of the union will be 8 bytes.
How to Declare Union Variables In C
The way we declare a structure variable in C language, in the same way, we will declare the union variable here.
We declare union variables so that union members can easily access and use it anywhere in our program.
Example -:
union item
{ int x;
float y;
char z;
};
Here union is the keyword and item is a new user-defined data type inside which with the help of int, float, and char there are three union members declared as x, y, and z. If we want to access these three members then for this we have to declare a variable of this item data type.
In C language, we can declare a union variable like a structure in two ways -:
- By using the union keyword inside the main() function.
- At the end of the union when defining a union.
Method 1 -: Inside the main() function by using the Union keyword
Let’s understand this better through an example.
Example -:
#include<stdio.h>
union item
{ int x;
float y;
char z;
};
void main()
{
union item it1, it2;
}
As I mentioned above, to access the union member, we have to declare the union variable. So here in this example, we have used union item it1, it2; in the main() function to access union members. By declaring two union variables named it1, and it2, with the help of which we can easily access the union member.
Method 2 -: At the end of the union while defining union.
Let’s understand this also through an example -:
Example -:
union item
{ int x;
float y;
char z;
}it1,it2;
- We can also declare the union variable like in the above example.
- The dot (.) operator is used to access the union member and if any union pointer variable wants to access the union member then -> (structure pointer operator) is used instead of the dot (.) operator.
Initialization of Union Members In C
We can initialize union members like this -:
#include<stdio.h>
union item
{ int x;
float y;
char z;
};
void main()
{
union item it1;
//initialization of each member separately
it1.x = 35;
it1.z = 'a';
it1.y = 15.5;
}
Accessing Members in Union
Like structure, we can also access the union member in these two ways. Let’s learn about these methods.
- by Dot(.) (member or dot operator)
- by -> (union pointer operator)
Method 1 -: by Dot(.) operator
Through the dot(.) operator, we can access union variables and initialize any value according to them and print them in the screen.
Examples -:
#include<stdio.h>
union item
{ int x;
float y;
};
void main()
{
union item it1;
//initialization of each member separately
it1.x = 10;
printf( " x = %d \n",it1.x);
it1.y = 15.5;
printf( " y = %f ", it1.y);
}
Output -:
x = 10
y = 15.5
Note -: We can access only one union member at a time. If we try to access more than one union member at a time then error comes.
Example -: if we try to access all union members at the same time
#include<stdio.h>
union item
{ int x;
float y;
char z;
};
void main()
{
union item it1;
//initialization of each member separately
it1.z = 'a';
it1.y = 15.5;
printf( " x = %c y = %f ", it1.z, it1.y);
}
Output -:
x = y = 15. 5
In this example, we wanted to print the stored values in x and y to the screen by accessing x and y simultaneously. But it did not happen. The compiler was able to print only the value stored in y because, as I mentioned above, in a union we can access only one union member at a time.
In the earlier example, we had accessed both the members separately and printed their values in the screen, so the output was absolutely correct.
Note -: If union variable is a pointer then we can access more than one member at a time.
Let’s now learn about the other way to access union member
Method 2: By -> (union pointer operator)
By -> (union pointer operator) we can easily access the union member. Let’s understand this through an example -:
Example Program
#include <stdio.h>
union test {
int x;
char y;
};
void main()
{
union test p1;
p1.x = 65;
// p2 is a pointer to union p1
union test* p2 = &p1;
// Accessing union members using pointer
printf("%d %c", p2->x, p2->y);
}
Output -:
65 A
In this example, we created two pointer variables p1, and p2, and used -> to access union members by pointer variable. Since the ASCII value of 65 is A, the result will print the value of p1 as 65 and the value of p2 as A.
Why We Need Union
A program can have different type of value, if we store these values in different memory space then memory consumption will be more. But if we use these values by replacing, then we will be able to store and use different types of values at the same place, this will save a lot of memory.
Union members use the same memory location to store data among themselves. Union is very useful in embedded programming.
Different Between Structure And Union In C
Structure | Union |
---|---|
The struct keyword is used to define a structure. | The union keyword is used to define union. |
Each struct member gets a different memory location to store the data | All Unio members get only one memory location to store data |
Changing the value of one data member does not affect the other data member. | A change in one data member affects other members. |
You can initialize the value by accessing more than one member at a time. | You can initialize the value by accessing only one member at a time. |
The size of the structure is the sum of the size of the structure member. | The size of the union is equal to the size of the largest member in the union. |
Each struct member gets space in memory. | Not every union member gets space in memory. |
It supports flexible array | It does not support flexible array |
Can access more than one member at a time. | Only one member can be accessed at a time. |
Advantages of Union
- It takes very less memory space than the structure
- Using union, you can only access the last member directly.
- Unions are more useful when you need to store the values of multiple data members at the same time, sharing the same memory location.
- Its size is equal to the size of the largest union member.
Disadvantages of Union
- Only one member can be accessed at a time.
- More than one union member cannot be used simultaneously.
- A memory location is shared between more than one union member.
Example Program of Union In C
#include <stdio.h>
union item
{
int x;
float y;
char ch;
};
int main( )
{
union item it;
it.x = 102;
printf("%d\n", it.x);
it.y = 20.2;
printf("%f\n", it.y);
it.ch = 'a';
printf("%c\n", it.ch);
return 0;
}
Output -:
102
20.2
A
Program Explanation
Since in union, we can access only one member at a time, so in this program, we access each member and input the value in them and print it on the screen.
If we tried to print the value simultaneously by accessing all three, then only the last value would be printed on the screen.
Read More -:
Conclusion
Friends, I hope you have found the answer to your question and you will not have to search about What is Union In C? How to use union in C language? And what is the difference between structure and union In C?
However, if you want any information related to this post or related to programming language, or computer science, then comment below I will clear your all doubts
If you want a complete tutorial on C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about Union In C
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.