Defining Union in C
Defining Union
There are two ways to define unions in C Programming:
- Anonymous union
- Named Union
Anonymous union
- An anonymous union is a union without a name.
- It is defined inside a structure or another union and can be used to group related data.
Anonymous unions are useful when a small amount of related data needs to be grouped together and accessed frequently.
Example:
struct employee {
char name[50];
int id;
union {
float salary;
int hours_worked;
};
};
Explanation
- We define a structure
employeethat has three members:name,id, and ananonymous union - The anonymous union has two members:
salaryandhours_worked. - The union is anonymous because it does not have a name.
- This allows us to access the
salaryandhours_workedmembers directly using thedotnotation, as if they were part of theemployeestructure.
Anonymous union - Example
Here's an example of an anonymous union in C
#include <stdio.h>
struct student
{
char name[50];
int id;
union
{ // define an anonymous union
float gpa;
int grade;
}; // the union does not have a name
};
int main()
{
struct student s;
// set values for the name, id, and gpa members of the student structure
strcpy(s.name, "John");
s.id = 123;
s.gpa = 3.5;
// print the values of the name, id, and gpa members of the student
// structure
printf("Name: %s\n", s.name);
printf("ID: %d\n", s.id);
printf("GPA: %f\n", s.gpa);
// set a value for the grade member of the anonymous union
s.grade = 90;
// print the value of the grade member of the anonymous union
printf("Grade: %d\n", s.grade);
return 0;
}
Output:
Explanation:
- We define a structure
studentthat has three members:name,id, andan anonymous union - The anonymous union has two members:
gpaandgrade. - The union is anonymous because it does not have a name.
- In the
mainfunction, we create a variablesof type student and set the values for the name, id, and gpa members. We then print the values of these members. - We then set a value for the grade member of the anonymous union using the
dotnotation, as if it were part of thestudentstructure. - We print the value of the
grademember using thedotnotation.
Because the union is anonymous, we can access the gpa and grade members directly using the dot notation, as if they were part of the student structure. This makes the code more concise and easier to read.
Named union
- A named union is a union with a name.
- It is defined separately from other data types and can be used in the same way as a structure.
Example:
union number {
int i;
float f;
double d;
};
Explanation
- We define a union number that has three members:
i,f, andd. - The union is named
number, and it can be used to store aninteger, afloat, or adoublevalue in the same memory location.
To declare a variable of type number, we use the following syntax:
union number num;
This creates a variable num of type number, which can be used to store an integer, a float, or a double value.
Named union - Example
Here's an example of a named union:
#include <stdio.h>
union number
{ // define a named union
int i;
float f;
double d;
};
int main()
{
union number n; // declare a variable of type number
// set values for the i, f, and d members of the number union
n.i = 10;
n.f = 3.14;
n.d = 2.71828;
// print the values of the i, f, and d members of the number union
printf("i: %d\n", n.i);
printf("f: %f\n", n.f);
printf("d: %lf\n", n.d);
return 0;
}
Output:
Explanation
- We define a named union
numberthat has three members:i,f, andd. - In the
mainfunction, we create a variablenof type number and set the values for thei,f, anddmembers using thedotnotation. - We then print the values of the
i,f, anddmembers using thedotnotation.
Because the union is named, we can declare variables of type number anywhere in our program, and the values of the i, f, and d members will be accessible using the dot notation.
Comparision - Anonymous Union and Named Union
| Feature | Anonymous Union | Named Union |
|---|---|---|
| Definition | Defined inside a struct or another union without a name | Defined separately with a name |
| Accessibility | Members can be accessed directly using the dot notation as if they were part of the enclosing struct or union | Members can only be accessed using the union name and the dot notation |
| Scope | The union is only accessible within the scope of the enclosing struct or union | The union can be used throughout the program |
| Memory allocation | Shares the same memory location for all its members | Shares the same memory location for all its members |
| Clarity | Useful for creating a compact and readable code, particularly when the union is only used once | Useful for creating a data type that can be used throughout the program, particularly when a more complex data structure is needed |