The typedef keyword allows programmers to create meaningful names for types. This is particularly useful when working with long structure declarations or complex pointers, making the code look cleaner and more professional.
The syntax follows a simple pattern: typedef [existing_type] [alias_name];
One of the most common uses of typedef is with struct. Without it, you must use the struct keyword every time you declare a variable. With it, you can treat the struct like a built-in type (like int or char).
Pointer syntax can become confusing, especially with multiple indirections. typedef can clarify your intent.
Balance or Node are more descriptive than float or struct MyNode.typedef int INT32; throughout your program and later need to change to a long, you only need to change it in one line.While #define is a preprocessor command that performs text replacement, typedef is interpreted by the compiler and is much safer for data types.
| Feature | typedef | #define |
|---|---|---|
| Execution | Compiler level (Type aware) | Preprocessor (Text swap) |
| Semicolon | Ends with a semicolon | No semicolon |
| Scope | Follows scope rules | Always global |
size_t or uint32_t. These are all typedef aliases created to ensure the code works identically across different computer architectures.