SoFunction
Updated on 2025-03-02

A brief introduction to the use of typedef in C and C++

typedef declaration, referred to as typedef, creates a new name for an existing type. For example, people often use typedef to write more beautiful and readable code. The so-called aesthetics means that typedef can hide clumsy syntax construction and platform-related data types, thereby enhancing portability and future maintainability.

There are generally two purposes for using typedef in programming. One is to give a variable a new name that is easy to remember and has clear meaning, and the other is to simplify some more complex type declarations.

When I was learning data structures, I found that I had forgotten a lot of knowledge I had learned before. After I found that my understanding of the keyword typedef in C/C++ was not in place, I read the textbooks used to learn C++, asked the Baidu girl, and read a lot of blogs about the usage of typedef. So I wanted to sort out what I understood.

1. Basic explanation

typedef is a keyword in C language and its function is to define a new name for a data type. The data types here include internal data types (int, char, etc.) and customized data types (struct, etc.).

There are generally two purposes for using typedef in programming. One is to give a variable a new name that is easy to remember and has clear meaning, and the other is to simplify some more complex type declarations.

two. usage

(1) Use typedef to declare a new type name instead of the existing type name. like:

typedef int Status //Specify the identifier Status represents the int typetypedef double DATE //Specify an identifierDATErepresentdoubletype

This is the following code equivalent:

int i; double j;
Status i;DATE j;

(2) Use typedef to name the array type:

typedef int NUM[100];//Declare NUM as an integer array type, can contain 100 elementsNUM n;//definitionnFor inclusion100an array of integer elements,nIt's the array name

(3) Declare a new name to a structure type:

typedef struct //The keyword typedef is used before struct, indicating that it is to declare the new type name{
int month;
int day;
int year; 
} TIME; //TIMEIt's a new type name,But not the new type,It's not a structure variable name

The new type name TIME of the newly declared represents a structure type specified above, so that the structure variable can be defined with TIME, such as:

TIME birthday;
TIME *P //pis a pointer to the structure type data

three. Note

(1) Using typedef only adds a type name to an existing type, without creating a new type. Just add a new name, and you can use this name to define the variable, such as using Status above to define the variable i; then the type of the i variable is int type.

(2) You can use typedef to declare new type name. But it cannot be used to define variables

Four. advantage

Using typedef type names is beneficial to the portability of the program. Sometimes programs rely on hardware features. For example, in a C++ system, use 2 bytes to store an int type variable and 4 bytes to store a long type variable. In another C++ system, int type variables are stored in 4 bytes. When a C++ program is ported from a C++ system that stores an int type variable with 2 bytes to a C++ system that stores an int type variable with 4 bytes, if the int type is originally declared with typedef, then for example:

Typedef int INTEGER ; //So that's what it meansTypedef long INTEGER ; //It can be changed to this after transplant

If it is not declared with typedef, then every place where the int type is defined must be changed. The larger the program, the greater the workload.

The above is a brief introduction to the use of typedef in C and C++ introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message. The editor will reply you in time. Thank you very much for your support for my website!