Question
I'm a beginner in C programming and I want to understand the difference between defining a structure with typedef and defining it without typedef.
These two examples seem to achieve the same result:
struct myStruct {
int one;
int two;
};
and
typedef struct {
int one;
int two;
} myStruct;
What is the actual difference between these two forms in C, and when would you use one over the other?
Short Answer
By the end of this page, you'll understand what a struct tag is in C, what typedef does, and why these two declarations look similar but are not exactly the same. You'll also learn the syntax developers use in real C codebases and how to choose the right form for readability and maintainability.
Concept
In C, struct and typedef solve related but different problems.
structdefines a structure type.typedefcreates an alias name for an existing type.
That means these are not identical operations.
1. Defining a struct without typedef
struct myStruct {
int one;
int two;
};
This creates a structure type with the tag myStruct.
To declare variables of that type, you must write:
struct myStruct a;
Notice that in C, the word struct is part of the type name when you use the tag.
2. Defining a struct with typedef
one;
two;
} myStruct;
Mental Model
Think of struct and typedef like a person and a nickname.
struct myStructis the person's full formal name.typedef ... myStructis giving that type a shorter nickname.
If you only create the formal name, you must always use the full name:
struct myStruct x;
If you only create the nickname, you use the nickname:
myStruct x;
If you create both, you can use either one.
Another way to think about it:
structcreates the actual structure type definition.typedefcreates a label you can use instead of the original type spelling.
typedef does not create a new kind of data by itself. It only gives an existing type another name.
Syntax and Examples
Core syntax
Struct with a tag only
struct Point {
int x;
int y;
};
struct Point p1;
Here, Point is a struct tag. You must write struct Point when declaring variables.
Typedef with an anonymous struct
typedef struct {
int x;
int y;
} Point;
Point p1;
Here, Point is a typedef alias. You use Point directly.
Typedef with a tagged struct
typedef struct Point {
int x;
int y;
} Point;
Point p1;
struct Point p2;
Step by Step Execution
Consider this example:
#include <stdio.h>
typedef struct {
int one;
int two;
} MyStruct;
int main(void) {
MyStruct a;
a.one = 5;
a.two = 7;
int total = a.one + a.two;
printf("%d\n", total);
return 0;
}
Step by step
1. The struct type is defined
typedef struct {
int one;
int two;
} MyStruct;
- A struct is defined with two members:
oneandtwo MyStructbecomes a typedef alias for that struct type
2. A variable is declared
MyStruct a;
- Memory is reserved for one struct variable named
Real World Use Cases
Structures are used whenever you need to group related data together.
Common examples
User records
typedef struct {
int id;
char name[50];
} User;
Used in:
- command-line tools
- file parsing
- database-like records in memory
2D points or geometry
typedef struct {
int x;
int y;
} Point;
Used in:
- graphics programs
- games
- coordinate calculations
Configuration data
typedef struct {
int port;
int debug;
} Config;
Used in:
- application settings
- server startup options
- embedded systems
API response models
status;
message[];
} Response;
Real Codebase Usage
In real C projects, the choice between struct X and typedef struct ... X often depends on team style, API design, and compatibility needs.
Common patterns
1. Public data models in header files
Libraries often use typedefs so users of the library get simple names:
typedef struct {
int code;
char text[64];
} ErrorInfo;
This keeps function signatures short:
ErrorInfo get_last_error(void);
2. Opaque structs for encapsulation
A common C pattern is to expose only a forward declaration in a header:
typedef struct FileHandle FileHandle;
The full struct definition is hidden in a .c file. This lets developers use pointers without seeing internal fields:
FileHandle *open_file( *path);
Common Mistakes
1. Thinking typedef creates a new struct kind
typedef does not create a brand new data structure. It only creates another name for a type.
typedef int Number;
Number is still just int.
The same idea applies to structs.
2. Mixing up tag names and typedef names
Broken example:
typedef struct {
int one;
int two;
} MyStruct;
struct MyStruct a; // wrong
Why it fails:
MyStructhere is a typedef alias- There is no struct tag named
MyStruct
Correct:
MyStruct a;
Or define both:
Comparisons
| Form | What it creates | How you declare a variable | Notes |
|---|---|---|---|
struct Point { ... }; | Struct tag Point | struct Point p; | Traditional C style |
typedef struct { ... } Point; | Typedef alias Point | Point p; | Anonymous struct with alias |
typedef struct Point { ... } Point; | Tag Point and typedef alias Point | struct Point p; or Point p; |
Cheat Sheet
Quick rules
struct X { ... };defines a struct tag namedX- Use it as
struct X variable; typedef struct { ... } X;creates a typedef alias namedX- Use it as
X variable; typedef struct X { ... } X;gives you both forms
Common patterns
Tag only
struct Person {
int age;
};
struct Person p;
Typedef only
typedef struct {
int age;
} Person;
Person p;
Tag + typedef
typedef struct Person {
int age;
} Person;
Person p1;
FAQ
What is the difference between struct and typedef struct in C?
struct defines a structure type with a tag, while typedef gives a type another name. A plain struct must be used as struct Name, but a typedef alias can be used as Name.
Does typedef struct create a new type?
Not in the same sense as defining a new kind of data. It creates an alias for a type. The underlying struct type is still the same.
Why do C programmers often use typedef with structs?
Mostly for convenience and readability. It removes the need to write struct every time the type is used.
Can I use struct MyType after typedef struct { ... } MyType;?
No. That form creates a typedef alias but no struct tag named MyType.
When should I use typedef struct Name { ... } Name;?
Use it when you want both a struct tag and a short alias. This is common in larger C projects and libraries.
Is this different in C++?
Mini Project
Description
Build a small C program that stores and prints information about a book. The goal is to practice defining a struct both conceptually and syntactically, and to see how a typedef makes declarations shorter in normal code.
Goal
Create a program that defines a Book structure, stores one book's data, and prints it using a function.
Requirements
- Define a structure with fields for title, author, and year.
- Use
typedefso the type can be written without thestructkeyword. - Create one variable of that type in
main. - Assign values to all fields.
- Write a function that accepts the struct and prints its contents.
Keep learning
Related questions
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.
Definition vs Declaration in C and C++: What’s the Difference?
Learn the difference between declarations and definitions in C and C++ with simple examples, common mistakes, and practical usage.
Difference Between #include <...> and #include "..." in C and C++
Learn the difference between #include with angle brackets and quotes in C and C++, including search paths, examples, and common mistakes.