Open In App

Anonymous Union and Structure in C

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C, anonymous unions and anonymous structures are the unnamed structures and unions whose members can be directly accessed without creating a variable. These are generally used when the members of the union or structure need to be accessed directly, without using a separate name for the union or structure.

Example:

C
//Driver Code Starts
#include <stdio.h>

//Driver Code Ends

struct A {
    int i;
    struct {
        int ii;
        char cc;
    };
};

int main() {
  
    // Define and name the anonymous union
    struct A a;
  	a.i = 1;
  	a.ii = 20;
    a.cc = 'C';
    
    printf("%d %d %c", a.i, a.ii, a.cc);

//Driver Code Starts

    return 0;
}
//Driver Code Ends

Output
1 20 C

Explanation: In the above code, we have created structure A inside which we have defined an unnamed structure whose members we have accessed directly later in the program as if they are the direct members of A. Similarly, we can define an anonymous union.

Syntax

The syntax of anonymous structure and union are same as the normal structs and unions but without a name.

union/structure {
type1 member1;
type1 member2;
.
.
} (variables if any);

The variable of anonymous structure can only be defined with the declaration as shown in the syntax

Examples of Anonymous Unions and Structures

The below examples illustrate how the anonymous structure and unions are used in C:

Anonymous Structure Outside Nesting

C
#include <stdio.h>

// Anonymous structure
struct {
    int ii;
    char cc;
} anon;

int main() {
  
    // Define and name the anonymous union
  	anon.ii = 20;
    anon.cc = 'C';
    
    printf("%d %c", anon.ii, anon.cc);

    return 0;
}

Output
20 C

Anonymous Union in Local Scope

C
#include <stdio.h>

int main() {
  
    // Define an anonymous union
    union {
        int age;
        float height;
    } person; 

    // Assigning values to the members
    person.age = 25; 
    printf("%d\n", person.age);

    person.height = 5.5; 
    printf("%.2f", person.height);

    return 0;
}

Output
25
5.50

Explanation: In this code, an anonymous union is defined inline inside the main() function without a name, containing two members: age (integer) and height (float). The person variable accesses these members, but since union members share memory, only one member can hold a value at a time.

Anonymous Union within a Structure

Anonymous unions can also be used within structures to allow flexible data representation.

C
#include <stdio.h>

struct Data {
    int id;

    // Anonymous union inside structure
    union {
        int i;
        float f;
    };
};

int main() {
    struct Data data;

    data.id = 1;
    data.i = 100;

    printf("%d\n", data.id);
    printf("%d\n", data.i);

    data.f = 5.5;
    printf("%.1f", data.f);

    return 0;
}

Output
1
100
5.5

Explanation: In this code, a structure Data contains an anonymous union with i and f. The main() function initializes the structure, assigns values to the union members, and prints the values. Since it's a union, both members share the same memory, and the last assigned value overwrites the previous one.

What about C++? 

Anonymous Unions and Structures are NOT part of C++ 11 standard, but most of the C++ compilers support them. Since this is a C only feature, the C++ implementations don't allow to anonymous struct/union to have private or protected members, static members, and functions.  


Similar Reads