Anonymous Structures and Unions in C
Anonymous Structures and Unions in C
The feature of defining struct and union anonymously has been found to be
extremely useful, especially in applications involving the creation of complex data
structures, hardware register mappings, etc. This allows for more straightforward
and readable code.
Anonymous Structure
An anonymous structure is a struct definition without a tag or a typedef. It is
usually nested within another struct.
struct type {
elem1;
elem2;
struct {
elem3;
elem4;
};
};
Example 1
In the code below, we have defined a structure with the name as employee. Inside
it, an anonymous struct is intended to hold date, month and year of birth of the
employee. In the example on nested structure, we had an internal dob structure.
Now we’ll use anonymous struct.
https://www.tutorialspoint.com/cprogramming/c_anonymous_structures_and_unions.htm 1/5
6/17/24, 12:05 AM Anonymous Structures and Unions in C
#include <stdio.h>
struct employee {
char name[10];
float salary;
struct {
int d, m, y;
};
};
int main(){
return 0;
}
Output
When you run this code, it will produce the following output −
Name: Kiran
Salary: 25000.000000
Date of Birth: 12-5-1990
Note that the "d", "m" and "y" elements of inner anonymous struct are accessed
directly.
Example 2
The outer struct type in the following example is student, which has a nested
anonymous struct to store the title and ID of the course.
https://www.tutorialspoint.com/cprogramming/c_anonymous_structures_and_unions.htm 2/5
6/17/24, 12:05 AM Anonymous Structures and Unions in C
#include <stdio.h>
struct student {
char name[10];
int age;
struct {
char coursettl[20];
int courseid;
};
};
int main(){
return 0;
}
Output
When you run this code, it will produce the following output −
Name: Kiran
age: 27
Course Title: C Programming Course ID: 1
Anonymous Union
An anonymous union is a special type of union that doesn't have a name. Unlike
regular unions, anonymous unions are primarily used to create unnamed members
that can be accessed directly without qualifying them with a union name.
https://www.tutorialspoint.com/cprogramming/c_anonymous_structures_and_unions.htm 3/5
6/17/24, 12:05 AM Anonymous Structures and Unions in C
struct type {
elem1;
elem2;
union {
elem3;
elem4;
};
};
Example
Anonymous unions do not have a name. The elements share the same memory
location. The members of the anonymous union can be accessed directly without
using a union name.
#include <stdio.h>
struct mystruct {
int var;
union {
int var1;
float var2;
char var3;
};
};
int main(){
data.var = 10;
data.var2 = 5.55;
https://www.tutorialspoint.com/cprogramming/c_anonymous_structures_and_unions.htm 4/5
6/17/24, 12:05 AM Anonymous Structures and Unions in C
return 0;
}
Output
mystruct.var: 10
anonymous union elements: 1085381018 5.550000 �
Memory Efficiency − Like regular unions, anonymous unions allow different data
types to share the same memory space, leading to more memory-efficient code,
especially useful in low-memory environments.
Ease of Initialization − They can be easier to initialize and use, as they do not
require the declaration of a union variable.
Note that anonymous struct or union types were not part of the C standard before
C11. Hence, their use in the code may lead to compatibility problems, if the target
system uses a compiler compliant with earlier standards.
https://www.tutorialspoint.com/cprogramming/c_anonymous_structures_and_unions.htm 5/5