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
employee
that has three members:name
,id
, and ananonymous union
- The anonymous union has two members:
salary
andhours_worked
. - The union is anonymous because it does not have a name.
- This allows us to access the
salary
andhours_worked
members directly using thedot
notation, as if they were part of theemployee
structure.
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
student
that has three members:name
,id
, andan anonymous union
- The anonymous union has two members:
gpa
andgrade
. - The union is anonymous because it does not have a name.
- In the
main
function, we create a variables
of 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
dot
notation, as if it were part of thestudent
structure. - We print the value of the
grade
member using thedot
notation.
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 adouble
value 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
number
that has three members:i
,f
, andd
. - In the
main
function, we create a variablen
of type number and set the values for thei
,f
, andd
members using thedot
notation. - We then print the values of the
i
,f
, andd
members using thedot
notation.
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 |