Skip to main content

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 an anonymous union
  • The anonymous union has two members: salary and hours_worked.
  • The union is anonymous because it does not have a name.
  • This allows us to access the salary and hours_worked members directly using the dot notation, as if they were part of the employee 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, and an anonymous union
  • The anonymous union has two members: gpa and grade.
  • The union is anonymous because it does not have a name.
  • In the main function, we create a variable s 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 the student structure.
  • We print the value of the grade member using the dot notation.
note

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, and d.
  • The union is named number, and it can be used to store an integer, a float, or a double 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, and d.
  • In the main function, we create a variable n of type number and set the values for the i, f, and d members using the dot notation.
  • We then print the values of the i, f, and d members using the dot notation.
note

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

FeatureAnonymous UnionNamed Union
DefinitionDefined inside a struct or another union without a nameDefined separately with a name
AccessibilityMembers can be accessed directly using the dot notation as if they were part of the enclosing struct or unionMembers can only be accessed using the union name and the dot notation
ScopeThe union is only accessible within the scope of the enclosing struct or unionThe union can be used throughout the program
Memory allocationShares the same memory location for all its membersShares the same memory location for all its members
ClarityUseful for creating a compact and readable code, particularly when the union is only used onceUseful for creating a data type that can be used throughout the program, particularly when a more complex data structure is needed