This blog is connected to the previous article "Program Environment and Preprocessing (1)"
content
1.4 Macro parameters with side effects
1.5 Comparison of macros and parameters
5.1 How header files are included
1.#define
1.1#define define identifier
#define name stuff
The chestnuts are as follows:
The constants represented by macros can be numbers, characters, strings, and expressions. The most common of these are numbers.
register int a=1; Explicitly declare that the variable must be stored in the register until the variable disappears. It is generally the default register, and in most cases it is not necessary to write register.
Note: Do not add ; when defining the identifier;
If it is added, it will lead to the following results, and NUM is replaced by 200;
1.2 #define definition macro
How to declare a macro definition
#define name(parament-list) stuff
Note that the parameter opening parenthesis must be immediately adjacent to the name. If there is a gap between the two, the parameter list will be interpreted as part of the stuff
The following is also an error-prone place
As can be seen from the above question, when using macro definitions, don't be stingy with parentheses
Two more notes
1. Macro, cannot achieve recursion
2. When preprocessing searches for symbols defined by #define, the contents of character constants are not searched
1.3 # and ## (to understand)
Below we can look at this code
It can be found that strings have the characteristics of automatic connection.
According to this feature, we can use # to turn a macro parameter into a corresponding string
#define PRINT(N, format) printf("the value of "#N" is "format"\n", N)
int main()
{
int a = 20;
double pai = 3.14;
PRINT(a, "%d");
PRINT(pai, "%lf");
return 0;
}
Use ## to combine the symbols on either side of it into one symbol
It allows macro definitions to create identifiers from separate pieces of text
#define CAT(name,num) name##num
int main()
{
int class105 = 200;
printf("%d\n", CAT(class, 105));
return 0;
}
1.4 Macro parameters with side effects
This shows that when a macro parameter appears more than once in a macro definition, if the parameter has side effects, you may be dangerous to use the macro, resulting in unpredictable results. Side effects are permanent effects of expression evaluation .
1.5 Comparison of macros and parameters
Attributes | #define define macro | function |
code length | Using the program is long most of the time | The function code only appears in one place, and the code in that place is called when used |
execution speed | faster | There are function calls and returns, relatively slow |
operator precedence | Additional parentheses are needed to prevent unpredictable results | Function parameters are only evaluated once and the result is passed to the function, and expression evaluation is more predictable |
Parameters with side effects | Arguments may be substituted into multiple places in the macro | Function parameters are only used once during evaluation, and the result is easy to control |
Parameter Type | The parameters of the macro are independent of the type | parameters and types |
debugging | Macros are not easy to debug | Can be debugged sentence by sentence |
recursion | cannot recurse | can recurse |
Macros are usually used to perform some simple operations.
1.6 Naming Conventions
capitalize macro names
Do not use all uppercase function names
2.#undef
#undef NAME
//If an existing name needs to be redefined, the old name needs to be removed first
3. Command line definition
Many C compilers provide the ability to allow symbols to be defined on the command line. The process used to initiate compilation
4. Conditional compilation
When compiling a program, it is very convenient if we want to compile or discard a statement, and we can satisfy it with conditional compilation instructions.
Let's look at the code below
#define MAX 0
int main()
{
#if defined (MAX)
printf("haha\n");
#endif
#if !defined(MAX)
printf("huhu\n");
#endif
#ifdef MAX
printf("huahua\n");
#endif
#ifndef MAX
printf("hlhl\n");
#endif
return 0;
}
Common compilation directives
1. #if constant expression
//...
#endif
2. Conditional compilation with multiple conditions
#if constant expression //...
#elif constant expression //...
#else constant expression //...
#endif constant expression //...
3. Determine whether it is defined
#if defined(symbol)
#ifdef symbol
#if !defined(symbol)
#ifndef symbol
4. Nested Directives
#if defined(OS_UNIX)
#ifdef OPTION1
unix_version_option1();
#endif
#ifdef OPTION2
unix_version_option2();
#endif
#endif
5. The file contains
5.1 How header files are included
#include"filename"
First look in the directory where the source file is located. If it is not found, the compiler will look for the header file (library function header file) in the standard location.
#include<filename>
Go directly to the standard path to find
Note: For library files, you can also use "" to include, but it is inefficient and it is not easy to distinguish between library files and local files
5.2 Nested file inclusion
Such nested file includes can be resolved with conditional compilation