About Lesson
typedef can be used to create an alias for a data type name. For example:
typedef int Marks;
This makes the name Marks a synonym or alias for int. The type Marks can be used in declarations, casts, etc. in exactly the same way as the type int.
Marks science, mathematics, overall;
typedefs are often used with structures:
typedef struct lnode { int value; struct lnode *next; } ListNode;
ListNode can be used now as type instead of struct lnode.
It’s important to note that typedef does not create a new data type, it merely adds a new name for some existing type.
Here are some reasons to use typedefs:
- If typedefs are used for data types that are machine-dependent, only the typedefs need to change when the program is moved. For example: size_t from “stddef.h”.
- It can improve the readability of a program. For example, ListNode is easier to understand than struct lnode in the above example.