struct and typedef struct are two C keywords. struct is used to define a structure that groups multiple data items of possibly different data types into a single collection. On the other hand, typedef struct is used to create an alias for an existing structure.
How to use the typedef struct in C
Key takeaways
typedefallows us to create simpler names for complex data types such asstruct, improving code readability.Use
typedefto avoid repeating thestructkeyword in variable declarations.
typedef structhelps us to maintain consistent type names across the codebase.
typedefstructures can be passed to functions either by value or by reference.

Calling elements by their full paths or full names can be cumbersome when writing the code. Imagine you are developing a social media app and need to manage user profile details. To do this, you can define a struct UserProfile data type to encapsulate all attributes of the user profile (e.g. name, age, gender, ID, etc.). Now, you have to write out the full type name like struct UserProfile every time you use it. Instead of repeatedly writing out the full type name like struct UserProfile, C programming language allows us to use a simpler alias like UserProfile or Profile.
What is the typedef keyword?#
C offers the typedef keyword to allow users to specify alternative simple and desired names for the primitive (e.g. int) and user-defined data types (e.g. struct). The typedef simplifies the use of complex data structures by allowing us to define a shorter, more convenient name for them. typedef can be a form of documentation by providing meaningful names for complex types.
“Good code is its own best documentation”—Steve McConnell
Where to put typedef struct in C?#
For structures, the C typedef keyword offers us to define a new type name for a structure, making the code more readable and maintainable. Here is the syntax to use typedef for the structure data type:
Using typedef struct results in a cleaner, more readable code, and saves the programmer keystrokes. However, it also leads to a more cluttered global namespace which can be problematic for large programs.
Remember,
typedefkeyword adds a new name for some existing data type but does not create a new type.
How to use typedef struct in C code#
The following code snippets first demonstrate the code without defining a struct using typedef, and then explain how to use a C keyword typedef for structures.
1. Structure declaration without using typedef#
The following code example demonstrates the struct declaration without using typedef.
2. Using the typedef keyword#
Note that if we use the typedef keyword with a struct, there is no longer a need to type struct again and again with every declaration of the variable of this type.
Method 1: Using typedef separately from the structure definition#
In the code below, the structure Point is defined separately using struct Point, and then a typedef is applied to create an alias Point for this structure. This allows us to declare variables of this structure type using just Point.
Method 2: Using typedef with the structure definition#
The typedef is applied directly to the structure definition itself. This combines the definition of the structure and the creation of the alias Point in a single statement.
How to pass a typedef structure to a function in C#
We can pass typedef structure to a function in C in the following way:
Define struct using
typedefkeyword.Declare and define a function that will accept the structure.
Decide which method to use to pass the structure—pass by value or pass by reference.
Call the function.
Here is the code example of passing a typedef struct to a function in C in both ways: passing by value and by reference.
Advantages of typedef struct in C#
Let’s discuss some of the typedef advantages for structures:
It simplifies type declarations.
It ensures consistency across the codebase.
If we need to change the structure or type, we only need to update the
typedefdefinition in one place, rather than updating every instance where the type is used.
It improves code readability.
Frequently Asked Questions
What is the difference between struct and typedef struct?
What is the difference between struct and typedef struct?
What is the difference between typedef and #define?
What is the difference between typedef and #define?
Why #define is used?
Why #define is used?
How can we use struct in C?
How can we use struct in C?
Is typedef struct needed in C?
Is typedef struct needed in C?
How can typedef be used with arrays?
How can typedef be used with arrays?
Does typedef create a new type?
Does typedef create a new type?
Is there any equivalent to typedef of C/C++ in Java?
Is there any equivalent to typedef of C/C++ in Java?
How do we define a typedef for a function pointer in C?
How do we define a typedef for a function pointer in C?
How do we use typedef for a union in C?
How do we use typedef for a union in C?