Question

Why `typedef struct` Is Common in C

cstructtypedef

Question

I often see C programs using structures written like this:

typedef struct
{
    int i;
    char k;
} elem;

elem user;

Why is this pattern used so frequently? Is there a specific reason for it, and in what situations is it especially useful?

Short Answer

By the end of this page, you will understand what typedef struct does in C, why many codebases use it, and when it is helpful versus unnecessary. You will also see how it compares to declaring a normal struct, what trade-offs it introduces, and how it appears in real C projects.

Concept

In C, a struct type normally has to be referred to with the struct keyword every time you use it.

For example:

struct Person {
    int age;
    char initial;
};

struct Person p;

Here, Person is a struct tag, not a standalone type name. That means you must write struct Person when declaring variables.

typedef lets you create an alias for an existing type. When used with a struct, it can create a shorter name:

typedef struct Person {
    int age;
    char initial;
} Person;

Person p;

Now Person can be used directly as a type name.

This matters because C treats struct Person and Person differently unless you define that alias yourself.

Why developers use it

Common reasons include:

  • Shorter syntax: Person p; is shorter than struct Person p;
  • Cleaner APIs: function signatures can look simpler
  • Consistency: many teams prefer all types to look similar
  • Readability: especially when a type is used often

Is it required?

No. It is a style choice in many cases.

These are both valid:

struct Point {
    int x;
    int y;
};

struct Point a;
typedef struct {
    int x;
    int y;
} Point;

Point a;

Why the anonymous form is common

Your example uses this pattern:

typedef struct {
    int i;
    char k;
} elem;

This creates a struct without a tag name and gives it only the typedef name elem.

That means:

  • You can write elem user;
  • You cannot write struct elem user; because there is no struct tag named elem

This style is common when the programmer only wants the typedef name and does not need the struct tag separately.

Why it matters in real programming

In larger C programs, types are used in many places:

  • function parameters
  • return values
  • arrays
  • pointers
  • library headers
  • data models

A shorter type name can make all of those easier to read.

However, some C programmers avoid typedef struct because they want it to stay obvious that the type is a struct. So this is partly about readability preferences and coding standards.

Mental Model

Think of a struct in C like a box design.

  • struct Person means: “use the box design named Person”
  • typedef gives that box design a simpler label, like a nickname

Without typedef, every time you ask for the box, you must say the full label:

struct Person user;

With typedef, you create a shortcut:

Person user;

So typedef does not create a new kind of box. It just gives the existing box design a shorter, more convenient name.

Take Quiz

Syntax and Examples

Basic struct without typedef

struct Book {
    int pages;
    char category;
};

struct Book b1;

You must write struct Book every time you declare a variable of that type.

Struct with typedef

typedef struct Book {
    int pages;
    char category;
} Book;

Book b1;

Now Book is a type alias, so you can declare variables more simply.

Anonymous struct with typedef

typedef struct {
    int i;
    char k;
} elem;

elem user;

This is similar to your example.

  • The struct has no tag name
  • elem is the typedef alias
  • You can use elem as the type name

Example with functions

Without typedef:

struct Point {
    int x;
    int y;
};

void print_point(struct Point p) {
    printf("(%d, %d)\n", p.x, p.y);
}

With typedef:

#include <stdio.h>

typedef struct {
    int x;
    int y;
} Point;

void print_point(Point p) {
    printf("(%d, %d)\n", p.x, p.y);
}

The second version is shorter and is often considered easier to read.

Important note

Many C style guides prefer this form when the struct may need both a tag and a typedef:

typedef struct Point {
    int x;
    int y;
} Point;

This gives you:

  • a struct tag: struct Point
  • a typedef alias: Point

That can be useful for forward declarations.

Step by Step Execution

Consider this code:

#include <stdio.h>

typedef struct {
    int i;
    char k;
} elem;

int main(void) {
    elem user;
    user.i = 42;
    user.k = 'A';

    printf("i = %d, k = %c\n", user.i, user.k);
    return 0;
}

Step by step

  1. typedef struct { ... } elem;

    • A struct type is defined.
    • elem becomes an alias for that type.
  2. elem user;

    • A variable named user is created using that struct type.
  3. user.i = 42;

    • The i field inside user is assigned 42.
  4. user.k = 'A';

    • The k field inside user is assigned 'A'.
  5. printf(...)

    • The program prints the values stored in the struct.

What changes if there is no typedef?

You would write:

struct elem {
    int i;
    char k;
};

struct elem user;

The behavior is the same. The difference is only in how the type name is written.

Real World Use Cases

typedef struct is common in real C programs when defining data models that are used repeatedly.

Common examples

  • Configuration objects
    • app settings
    • network options
    • parser settings
  • Domain models
    • users
    • orders
    • files
    • messages
  • Data structures
    • linked list nodes
    • queues
    • trees
    • stacks
  • Library APIs
    • handles
    • context objects
    • request/response structs

Example: configuration struct

typedef struct {
    int port;
    int debug;
} Config;

void start_server(Config cfg) {
    /* ... */
}

This reads more naturally than repeatedly writing struct Config everywhere.

Example: linked list node

typedef struct Node {
    int value;
    struct Node *next;
} Node;

This form is especially useful because the struct tag Node is needed inside the struct itself as struct Node *next;, while the typedef alias Node makes outside usage shorter.

Take Quiz

Real Codebase Usage

In real codebases, typedef struct is often used as part of broader style patterns.

1. Public API types

Libraries often expose types with simple names:

typedef struct Buffer Buffer;

Then the full struct definition may appear only in the .c file. This is a common encapsulation pattern.

2. Opaque structs

A header may declare a type without exposing its fields:

typedef struct Database Database;

Users of the API can work with Database * pointers, but cannot access internal fields directly.

3. Frequently used domain types

If a project uses a type in many functions, typedef reduces repetition:

typedef struct {
    int id;
    char name[50];
} User;

int save_user(User *user);
int delete_user(User *user);

4. Readable function signatures

Code often becomes cleaner in declarations and callbacks:

typedef struct Request Request;

typedef int (*RequestHandler)(Request *req);

5. Team style conventions

Some teams typedef almost every struct for consistency. Others avoid it to keep the underlying kind of type visible. Both approaches exist in professional code.

Common Mistakes

1. Thinking typedef creates a new type

It does not create a brand-new type in the same way a struct definition does. It mostly creates an alias.

typedef int Number;

Number is just another name for int.

2. Confusing a struct tag with a typedef name

This code defines only a struct tag:

struct Person {
    int age;
};

Person p; /* Error: Person is not a typedef name */

You must write:

struct Person p;

Or create a typedef:

typedef struct Person {
    int age;
} Person;

3. Using an anonymous struct when a tag is needed later

Broken example:

typedef struct {
    int value;
    struct Item *next;
} Item;

This fails because struct Item was never declared as a tag.

Correct version:

typedef struct Item {
    int value;
    struct Item *next;
} Item;

4. Overusing typedef for everything

Too many aliases can hide what a type really is.

typedef unsigned long int ULINT;

This may reduce clarity instead of improving it.

5. Assuming all C codebases prefer typedefs

Some do, some do not. Always check the project style guide.

6. Forgetting that C and C++ differ here

In C++, you can usually write:

struct Person {
    int age;
};

Person p;

In C, that requires a typedef or the struct keyword.

Comparisons

ApproachExampleProsCons
Plain struct tagstruct Person p;Explicit that it is a struct, traditional C styleMore typing
typedef struct { ... } Person;Person p;Short and cleanNo struct tag available later
typedef struct Person { ... } Person;Person p; or struct Person p;Flexible, works well with forward declarationsSlightly more verbose

typedef struct { ... } Name; vs typedef struct Name { ... } Name;

FormWhen to use it
typedef struct { ... } Name;When you only need the typedef name and do not need a struct tag
typedef struct Name { ... } Name;When the type may need self-references, forward declarations, or public API declarations

C vs C++

LanguageStruct usage
CMust write struct Name unless a typedef exists
C++Struct names automatically become type names
Take Quiz

Cheat Sheet

Quick reference

Plain struct

struct Point {
    int x;
    int y;
};

struct Point p;

Struct with typedef alias

typedef struct Point {
    int x;
    int y;
} Point;

Point p;

Anonymous struct with typedef

typedef struct {
    int x;
    int y;
} Point;

Key rules

  • In C, struct Name is not the same as Name
  • typedef creates an alias for a type
  • Anonymous structs have no tag name
  • Self-referential structs usually need a tag

Good default pattern

typedef struct Node {
    int value;
    struct Node *next;
} Node;

Use typedef struct when

  • the type is used often
  • you want cleaner function signatures
  • you are designing library APIs
  • you want an opaque type in a header

Be careful when

  • the project style guide prefers plain struct
  • the struct needs to refer to itself
  • too many typedef aliases make code harder to understand

FAQ

Why do C programmers use typedef struct so often?

Mostly for readability and convenience. It allows Point p; instead of struct Point p;.

Is typedef struct required in C?

No. It is optional. You can use plain struct declarations without typedef.

What is the difference between struct Name and Name in C?

struct Name is a struct tag reference. Name is only valid if you created a typedef with that name.

Why does typedef struct { ... } Name; not create struct Name?

Because that form defines an anonymous struct. It gives the type an alias Name, but no struct tag.

When should I avoid anonymous typedef struct declarations?

Avoid them when the struct needs self-references, forward declarations, or a named tag for API design.

Is this different in C++?

Yes. In C++, a struct name automatically becomes a type name, so typedef is usually unnecessary for that purpose.

Which style is better: plain struct or typedef struct?

Neither is universally better. It depends on readability, team conventions, and whether the struct needs a tag.

Take Quiz

Mini Project

Description

Build a small contact record type in C to practice declaring, using, and passing structs with typedef. This project shows how typedefs can make everyday code cleaner when a custom type is used in multiple functions.

Goal

Create a Contact type, store data in it, and print the record using helper functions.

Requirements

  • Define a struct for a contact with at least a name, age, and initial
  • Use typedef so the type can be written without the struct keyword
  • Write a function that prints a contact
  • Create at least one contact variable in main
  • Assign values to the fields and display them
Take Quiz

Keep learning

Related questions

View more