Macro-definition Technique of C language-contains different header files multiple times.
1. the header file is defined as follows:
/* Declears in "funcs. h "*/
FUNC_1 (id_fun0000001)
FUNC_1 (id_fun0000002)
FUNC_2 (ID_FUN2_001)
FUNC_2 (ID_FUN2_002)
2. Multiple include header files
# Define FUNC_1 (opt) opt,
# Define FUNC_2 (opt)
Enum aaa {
# Include "funcs. h"
ID_FUN1_END
};
# Undef FUNC_1
# Undef FUNC_2
# Define FUNC_1 (opt)
# Define FUNC_2 (opt) opt,
Enum bbb {
# Include "funcs. h"
ID_FUN2_END
};
# Undef FUNC_1
# Undef FUNC_2
In this way, after macro translation, we define the two enum as follows:
Enum aaa {
Id_fun0000001,
Id_fun0000002,
ID_FUN1_END
};
Enum bbb {
ID_FUN2_001,
ID_FUN2_002,
ID_FUN2_END
};
(C) Define the macro to prevent repeated inclusion of this header file. What is this sentence?
When one C language program is composed of multiple source program files, because each source program file has # include <...>
# Include "... h". In the end, they may form cross-inclusion and Repeated inclusion, resulting in errors.
To avoid this situation, you can use the fixed macro method to wrap each header file.
At the beginning of the formal compilation, the compiler now processes macros, that is, the "pre-compilation command ".
Use a macro (line starting with #) to package the header file content:
# Ifndef _ HEADER_One_H _ // indicates that the macro starts the row. If _ HEADER_One_H _ is not defined, it enters; otherwise, it exits.
# Define _ HEADER_One_H _ // define _ HEADER_One_H _//
Header1.h
Header file content
# Endif // macro end line
In C language, what are the differences between preprocessing File Inclusion (# include) and macro definition (# define? Yes
# Include is used to call included header files.
# Define is a defined macro, such as a constant.