C Structures, Unions and Enums

Intermediate
17 min read
Updated: Nov 2026
1 example
11 guides

Definition and Usage

User-defined types let you model real data. A structure (struct) groups related fields of different types. A union stores several members in the same memory. An enumeration (enum) names a set of integer constants, typedef creates type aliases, and bit-fields pack small values into a few bits. This section covers all of them with nested structures, arrays of structures, pointers to structures, and passing structures to functions.

Use -> with pointers.

If p is a pointer to a struct, p->field is shorthand for (*p).field. You will use it constantly with linked lists and trees.

Learning Path

Work through the stages in order, or jump to a topic from the index below (11 tutorials).

C Examples

A quick taste of structures, unions & enums. Compile with gcc file.c -std=c11 -Wall -o out.

Example 1 — A structure for a student record

Group a name, age, and grade point average.

C
#include <stdio.h>

struct Student {
    char name[20];
    int age;
    double gpa;
};

int main(void) {
    struct Student s = {"Ava", 20, 3.8};

    printf("%s, %d, %.1f\n", s.name, s.age, s.gpa);
    s.age++;
    printf("next year: %d\n", s.age);
    return 0;
}

How It Works

  1. struct Student defines a new type with three fields.
  2. Braces initialize the fields in order.
  3. The dot operator reads and writes members.

C Structures, Unions and Enums Topics

Every guide in this section with a one-line summary (11 tutorials).

Structures

6 tutorials

Group related fields into one type.

TopicDescriptionTutorial
StructuresDefine a struct type, create variables, and use the dot operator.Try it
Structure membersRead, write, initialize, and measure the members of a struct.Try it
Nested structuresPut one struct inside another to model compound data.Try it
Arrays of structuresStore many records in an array and loop over them.Try it
Pointers to structuresUse the arrow operator to reach members through a struct pointer.Try it
Structures and functionsPass structs by value or pointer and return them from functions.Try it

Unions

2 tutorials

Share one block of memory between several members.

TopicDescriptionTutorial
UnionsStore different types in the same memory, one at a time.Try it
Structures vs unionsCompare memory use and behavior of structs and unions.Try it

Enums, typedef, and bit-fields

3 tutorials

Name constants, alias types, and pack bits.

TopicDescriptionTutorial
EnumerationsGive names to related integer constants with enum.Try it
typedefCreate shorter or clearer names for existing types.Try it
Bit-fieldsPack small values into a specific number of bits inside a struct.Try it

Edge Cases & Pitfalls

Common mistakes when learning structures, unions & enums in C.

semi

Missing semicolon after }

A struct definition must end with ; after the closing brace.

cmp

Comparing structs with ==

C cannot compare structs with ==. Compare field by field or use memcmp carefully (padding can differ).

pad

Assuming sizeof is the sum of fields

The compiler inserts padding for alignment, so sizeof(struct) can be larger than the sum of its members.

Key Takeaways

  • struct: group related fields.
  • union: share one memory block.
  • enum and typedef: readable names.

One line: User-defined types let you model real data.

Frequently Asked Questions

Each structure member has its own storage, so a struct is as large as all members combined. All union members share the same memory, so a union is as large as its biggest member and only one member holds a valid value at a time.
Yes. Nested structures are common, for example a Rectangle made of two Point members.
typedef lets you write Point instead of struct Point, which shortens declarations and hides the tag.

Next: Structures

Define a struct type, create variables, and use the dot operator.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful