Typedef
I. Definition
Typedef: Declares custom data types, and works with various original data types to simplify programming.
Ii. Purpose
1. Ease of transplantation
Typedef int int32;
Int32 A; // define an integer variable
(Int32) A; // forced conversion type
Why does an int have to be int32 with the same name?
A: A microprocessor, Int, 16 bits, long, 32 bits
B microprocessor short 16-bit, int 32-bit, long 64-bit
Originally a processor program:
Typedef int int16;
Typedef long int32;
To port to B processor, you only need to change these new names, instead of changing the entire program.
Typedef short int16;
Typedef int int32;
2. Help struct in the old C code.
In the old C code, struct must be included when the struct object is declared.
For example, struct Node
{
Int data;
Struct node next; // struct must be included in C; struct can be omitted in C ++.
};
Write
Typedef struct Node
{
Int data;
} Node_re;
Node_re root;
3. Define an alias of an existing type
Typedef char * pchar;
Pchar Pa, PB;
When typedef is declared as a pointer, it is prone to problems. Therefore, we recommend that you add a const to the final typedef name as long as typedef is declared for the pointer so that the object referred to by the pointer will not be modified.
Typedef const char * pchar;
Define
1. Definition: Define a macro, that is, a identifier can be used to represent a string.
Ii. Usage:
1. Convenient program modification
Define constants that are frequently used in a program. In this way, you do not need to modify the entire program when the constant changes.
Example: # define PI 3.141592653 // you do not need to enter 3.141592653 for each time and directly enter pi
2. Improve program running efficiency
In C language, functions can make programs more modular, easy to organize, and can be reused.
However, when calling a function, you need to retain the field of the called function, so that the subfunction can return to continue execution after execution. It takes some time to restore the called function after the sub-function is executed.
If many sub-functions are executed, the conversion time can be ignored. However, if the number of executed functions is small, the overhead is relatively large.
Example: # define s (a, B) a * B // only a multiplication operation
Difference between define and typedef
Typedef is just a new name (only an alias) for the identifier to increase readability, and # define was originally used to define constants in C, to C ++, the appearance of const, Enum, and inline gradually makes it an alias tool. Sometimes it is easy to figure out which one should be used for typedef and which one should be used, for example, a statement such as # define int, which one can be used for the same purpose? I advocate the use of typedef, because in many early C compilers, this statement was invalid, but today's compiler has expanded. To be as compatible as possible, # define is generally followed to define "readable" constants and macro statement tasks, while typedef is often used to define keywords and lengthy type aliases.
Macro definition is just a simple string replacement (in-situ extension), while typedef is not in-situ extension, and its new name has a certain encapsulation, so that the new name identifier has a more convenient function to define variables. See the third line of the first code:
Typedef (int *) pint;
# Define pint2 int *
Same effect? Actually different! Differences in practice:
The effect of pint a, B; is the same as that of int * A; int * B; that is, two integer pointer variables are defined.
Pint2 a, B; has the same effect as int * a, B; indicates defining an integer pointer and an integer variable.