Keywords“typedef ” in C language is a very important keyword that is not widely used but is critical. It is used to create a new alias for existing data types, facilitating the portability and management of various programs. I often use it in the following aspects:
1) Simplifying complex type declarations: When encountering complex array types, pointer types, or structure types, using typedef to redefine a new type name can make the code cleaner, more readable, maintainable, and portable.
Here, we define a new type for a student, with the following application example:
// Define a structure for a student
typedef struct
{
char Name[50];
int Age;
Int Grade;
} STUDENT;
// This omits the function description file, saving document space
int main(void)
{
STUDENT MesAlice = {“Alice”, 25,3}; // Define a student’s structure data
printf(“Name: %s, Age: %d, Grade: %d\n”, MesAlice.Name, MesAlice.Age, MesAlice.Grade);
return 0;
}
1) Enhancing code portability: On different systems or platforms, the length of certain data types may vary. By usingtypedef to define custom data type aliases, you can easily modify the underlying data type when needed without having to change each instance throughout the code.
#include <stdio.h>
#include <string.h>
// Use typedef to define custom data type aliases
Typedef unsigned char BOOLEAN;
Typedef unsigned char INT8U;
Typedef signed char INT8S;
Typedef unsigned int INT16U;
Typedef signed int INT16S;
Typedef unsigned long INT32U;
Typedef signed long INT32S;
// This omits the function description file, saving document space
INT16U FindMaxValue(INT16U * Arr, INT16U Size)
{
INT16U Count = 0; // My habit is to initialize variables right after defining them
INT16U MaxValue = 0; // My habit is to initialize variables right after defining them
if (Size == 0)
{
return 0;
}
MaxValue = *(Arr);
for (Count = 1; Count < Size;Count++)
{
if (*(Arr+Count) > MaxValue)
{
MaxValue = *(Arr+Count);
}
}
return MaxValue;
}
// This omits the function description file, saving document space
INT16U main(void)
{
INT16U Num[8] = {10,20,150,60,1,9,200,18};
INT16U MaxValue = 0;
MaxValue = FindMaxValue(&Num,8);
printf(“Max Value: %d\n”, MaxValue);
return 0;
}
In both functions, all use INT16U to indicate 16-bit unsigned data. If it is signed data, you can use INT16S, making this function both simple and convenient to distinguish, and throughout the code, there is no longer the need to use int, char, and various complex structure data, as shown below:
typedef struct
{
char Name[50];
int Age;
Int Grade;
} ;
This way of defining new types enhances the efficiency of input code, saves space, increases code readability, and avoids repeatedly typing or copying and pasting the same structure. If there is an error in the variable names within the structure, it results in two different structures.
Are the following two structures the same?

There is also a very worthwhile application to learn from — the variable MaxValue, which is used in both functions. Would you use it like this?
When I develop programs, I have a particularly good habit of initializing all defined variables and pointers immediately after their definition. This habit has been with me for over 20 years. Many friends find it troublesome, but I have always insisted on it. If it were you, would you stick to it?
