In C programming language, union is a special user defined data type that allows to store different data types in the same memory location.
“union” uses the same memory location for multiple-purpose.
Or
In a union data type, single variable, i.e., same memory location, can be used to store multiple types of data.
In c programming union is similar to the structure but major difference is that it requires less memory than structure.
Apart from this difference, you can define a union with many variables, but only one member can contain single value at any given time.
Defining a Union
We can define union with “union” keyword. “union” defines a new user defined data type with one or more than one member.
Syntax of union definition:
1 2 3 4 5 6 | union union_name { member definition; member definition; ……… ……… } [one or more union variables]; |
In the union’s definition at the end & before the final semicolon, you can specify one or more union variables but it is optional.
Example:
1 2 3 4 5 | union Book { int i; float f; char c[30]; } book1,book2; |
Above define a union type named book having three members i, f, and c.
In the above example variables of Book is book1 and book2.
These variables can store an integer, a floating-point number, or a string of characters.
During the union definition, memory occupied by a union variable will be large enough to hold the largest member of the union.
In the above example, user defined data type Book will occupy 30 bytes of memory space because this is the maximum space which can be occupied by a character string.
Example: Write a program to store and print the following information such as name, roll number, class and section of the student using union.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include<stdio.h> union Student { char name[30]; char section[1]; int rollno; int Class; }s1; int main() { printf("Enter the name, rollno, class & section of student"); scanf("%s", &s1.name); scanf("%d", &s1.rollno); scanf("%d", &s1.Class); scanf("%s",&s1.section); printf("\n Student details"); printf("\n Name = %s", s1.name); printf("\n Rollno = %d", s1.rollno); printf("\n Class = %d", s1.Class); printf("\n Section = %s",s1.section); return 0; } |
OUTPUT
1 2 3 4 5 6 7 8 9 10 | Enter the name, rollno, class & section of student Aditya // enter by user 23456 // enter by user 12 // enter by user A // enter by user Student details Name = A Rollno = 65 Class = 65 Section = A |
In the above example, we can see that the values of name, rollno, and Class 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 Section is getting printed very well.
Note: In union, one variable can contain a single value at any given time which is the main purpose of having unions −
Correct Implementation of Above program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include<stdio.h> union Student { char name[30]; char section[1]; int rollno; int Class; }s1; int main() { printf("Enter Student details "); printf("\n Enter name "); scanf("%s", &s1.name); printf("\n You enter student name = %s", s1.name); printf("\n Enter roll no "); scanf("%d", &s1.rollno); printf("\n You enter student Rollno = %d", s1.rollno); printf("\n Enter class"); scanf("%d", &s1.Class); printf("\n You Enter Class = %d", s1.Class); printf("\n Enter section"); scanf("%s",&s1.section); printf("\n You enter student section = %s",s1.section); return 0; } |
OUTPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 | Enter Student details Enter name Aditya \\ enter by user You enter student name = Aditya Enter roll no 23456 \\ enter by user You enter student Rollno = 23456 Enter class 12 \\ enter by user You Enter Class = 12 Enter section A \\ enter by user You enter student section = A |