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.