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
Mental Model
Think of a struct in C like a box design.
struct Personmeans: “use the box design namedPerson”typedefgives 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.
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
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
-
typedef struct { ... } elem;- A struct type is defined.
elembecomes an alias for that type.
-
elem user;- A variable named
useris created using that struct type.
- A variable named
-
user.i = 42;- The
ifield insideuseris assigned42.
- The
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 {
value;
} Node;
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);
;
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:
Comparisons
| Approach | Example | Pros | Cons |
|---|---|---|---|
| Plain struct tag | struct Person p; | Explicit that it is a struct, traditional C style | More typing |
typedef struct { ... } Person; | Person p; | Short and clean | No struct tag available later |
typedef struct Person { ... } Person; | Person p; or struct Person p; | Flexible, works well with forward declarations | Slightly more verbose |
typedef struct { ... } Name; vs typedef struct Name { ... } Name;
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 Nameis not the same asName typedefcreates an alias for a type- Anonymous structs have no tag name
- Self-referential structs usually need a tag
Good default pattern
value;
} Node;
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 is usually unnecessary for that purpose.
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
typedefso the type can be written without thestructkeyword - Write a function that prints a contact
- Create at least one contact variable in
main - Assign values to the fields and display them
Keep learning
Related questions
Array-to-Pointer Conversion in C and C++ Explained
Learn what array-to-pointer conversion means in C and C++, how array decay works, and how it differs from a pointer to an array.
Building More Fault-Tolerant Embedded C++ Applications for Radiation-Prone ARM Systems
Learn practical C++ and compile-time techniques to reduce soft-error damage in embedded ARM systems exposed to radiation.
C Pointer to Array vs Array of Pointers: How to Read Complex Declarations
Learn the difference between pointer-to-array and array-of-pointers in C, plus a simple rule for reading complex declarations correctly.