C StructuresLesson 40Free preview
C typedef
This lesson is part of C · All levels
Summary
Typedef creates a new alias for an existing type so that code is easier to read and more portable across platforms. The classic is used to shorten struct declarations, for example typedef struct {...} students; so there is no need to write the word struct repeatedly. typedef does not create new types in memory, it only shorten the writing of the old type.
Key points
- The basic syntax is typedef type_long_new name; for example, typedef unsigned long ulong;
- For struct, typedef makes code more concise because the type name can be used without the word struct.
- Typedef is useful for long types such as size_t, int32_t, and platform-specific types.
- The typedef doesn't change the size or behavior of the original type, it's just an alias.
- Be careful with typedef pointers such as typedef int *IntPtr; as they are inherently attached when used with other declarators.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.