typedef和#define
define does not participate in compilation and is replaced during preprocessing .
typedefs participate in compilation and linking. Typedef is a rename, which can rename enum structures, etc., to improve code cleanliness.
First, the usage of typedef
In the C language, typedef is often used to define an alias for an identifier and a keyword. It is part of the language compilation process, but it does not actually allocate memory space. Examples are:
typedef int INT;
typedef (int*) pINT;
Typedef can enhance the readability of programs and the flexibility of identifiers, but it also has disadvantages such as "non-intuitiveness".
Second, the usage of define
define is a macro definition statement, which is usually used to define constants (including no parameters and with parameters), as well as to implement those macros that "seem to be kind on the surface, but behind a long list". It is not performed in the compilation process itself, but It is done before this (preprocessing process), but it is therefore difficult to find potential bugs and other code maintenance issues, examples of which are like:
define FALSE 0
define TRUE 1
define Add(a,b) ((a)+(b));
define Loop_10 for (int i=0; i<10; i++)
3. The difference between typedef and #define
- #define belongs to the preprocessor, and typedef belongs to the compiler.
- #define is replacement, typedef is rename
Personal conclusion: both of these are rarely used