In the C/C++ macro, the function of # is to stringify the macro parameters that follow it. Simply put, the macro variable referenced by it is replaced with a double quotation mark. The ## connection symbol consists of two hashes whose function is to connect two sub-connections in a macro definition with parameters to form a new substring. But it can't be the first or last substring. #include Local macro parameters that are useful for # or ## in the macro definition are not expanded. For example, INT_MAX in _STRI(INT_MAX) will not be expanded to 2147483647. If you want to expand the macro parameters, you need to add an intermediate conversion macro: #define STRI(s) _STRI(s)cout << STRI(INT_MAX) << endl; // prints : 2147483647 The purpose of adding this macro is to expand all the macro parameters in this layer, then the macro in the conversion macro can get the corresponding macro parameters. Next, let's take a look at some of the uses of pre-compilation directives to create conditional compilation parameters to control code compilation. Operations that include header files, usually in two formats: #include <> and "" indicate that the compiler has different order in the search for header files: <> means to start the search from the system directory, then search the directory listed in the PATH environment variable, do not search the current directory "" means to start searching from the current directory, then the directory listed by the system directory and the PATH environment variable. Therefore, the system header file generally uses <>, and the user-defined one can use "" to speed up the search. In addition, when you write more code, you will find that some header files have hidden dependencies, so be sure to pay attention. The Google C++ Style Guide also emphasizes the use of standard header file inclusion order to enhance readability and avoid hidden dependencies: 1 Related files (priority location, such as dir2/foo2.h) 2 C system files 3 C++ system files 4 .h files of other libraries 5 .h files in this project // structure 1#if constant_expression#else#endif// structure 2#if constant_expression#elif constant_expression#endif The structure here is similar to the common if...else and if...else if...else statements. When the condition after #if is non-zero (true), compile #if and #else or #elif The code, otherwise compile the code between #else and #endif (or determine if the condition after #elif is non-zero (true), decide whether to compile the code between #elif and #endif). #if 1 cout << "Hello world!" << endl;#else cout << "Nice to meet you!" << endl;#endif// prints : Hello world! #if 1 cout << "Hello world!" << endl; #elif 1 cout << "Nice to meet you!" << endl; #endif// prints: Hello world!// Nice to meet you! 4 #define,#undef,#ifdef,#ifndef usage #define is a macro definition method that everyone is familiar with. The usage structure is: // #define identifier replacement-code#define PI 3.1415926#define ADD(x,y) (x + y) #undef As the name suggests, it is to cancel the macro that has been defined before, if the identifier is not currently defined as a macro name, the directive will be ignored: // #undef identifier#undef PI The opposite of #ifdef and #ifndef means that if the macro is defined, the corresponding code is compiled; the latter is compiled if the macro is not defined. The general structure is: /* * #ifdef identifier * #else or #elif * #endif **/ #define DEBUG#ifdef DEBUG cout << "This is a debug message." << endl;#endif// prints : This is a debug message ./* * #ifndef identifier * #else or #elif * #endif **/ #ifndef DEBUG cout << "This is a debug message." << endl;#endif// prints nothing In programming, in order to avoid redefinition of header files, it is often used to solve the problem with #define: #ifndef MY_HEADER_FILE_H#define MY_HEADER_FILE_H// ...class MyHeaderFile { // ....};#endif // MY_HEADER_FILE_H In addition, there is the usage of #pragma once, as long as you add this directive at the beginning of the header file to ensure that the header file is compiled once. (In all preprocessing directives, the #pragma directive is probably the most complex. Its role is to set the state of the compiler or to instruct the compiler to perform some specific actions. This article does not cover much.) 5 #line usage The #line command is used to change the values ​​of the __LINE__ and __FILE__ variables. __FILE__ and __LINE__ describe the current file and the number of rows being read. // #line line-number filenameint main() {#line 10 "main.cpp" cout << __FILE__ << " " << __LINE__ << endl; }// prints : main.cpp 10 6 #error usage #error will directly cause the program to stop compiling and output the specified error message: // #error message#ifndef VERSION#error Version number not specified.#endif// The compiler will halt compiling and return with the specified error message: // fatal error C1189: #error : Version number not specifi Non-Contact Detector,Non Contact Voltage Tester,Voltage Tester Pen,Non Contact Voltage Detector YINTE TOOLS (NINGBO) CO., LTD , https://www.yinte-tools.com
Detailed explanation of the usage and function of #conditional statement in C++ programming
The role and usage of 1 # and ##