C/C++ #define definition using #

1. # Convert macro parameters to strings during precompilation

#define PRINT_MACRO_HELPER(x) #x
#define PRINT_MACRO(x) PRINT_MACRO_HELPER(x)
#define PRINT_ANOTHER_MACRO(x) #x"="PRINT_MACRO_HELPER(x)
char *str=PRINT_MACRO_HELPER(1235982536);
char *str2=PRINT_MACRO(i);
char *str3=PRINT_ANOTHER_MACRO(i);
printf("the string of str is %s,the string of str2 is %s ,the string of str3 is %s\n",str,str2,str3);
输出:
the string of str is 1235982536,the string of str2 is i,the string of str3 is i=i

2. Macro connector ##


It is used to connect two substrings (tokens) in a macro definition with parameters to form a new substring; but it cannot be the first or last substring. The so-called substring (token) refers to the smallest syntax unit that the compiler can recognize. However, "##" cannot be arbitrarily glued to any character, and must be a legal C language identifier. In a single macro definition, the "#" or "##" preprocessing operator can appear at most once. Problems arise if the order of evaluation associated with the '#' or '##' preprocessing operators is not specified. To avoid this problem, only one of the operators can be used in a single macro definition (ie, one '#' or one '##', or neither). Try not to use "#" and "##" unless absolutely necessary.

For example: the macro definition is #define XNAME(n) x##n, and the code is: XNAME(4), then during precompilation, the macro finds that XNAME(4) matches XNAME(n), then let n be 4, Then change the content of n on the right to 4, and then replace the entire XNAME(4) with x##n, that is, x4, so the final result is that XNAME(4) becomes x4. As shown in the following example:
 

#include <stdio.h>
#define XNAME(n) x##n
#define PRINT_XN(n) printf("x" #n " = %d\n", x##n);
int main(void)
{
    int XNAME(1) = 14; // becomes int x1 = 14;
    int XNAME(2) = 20; // becomes int x2 = 20;
    PRINT_XN(1);       // becomes printf("x1 = %d\n", x1);
    PRINT_XN(2);       // becomes printf("x2 = %d\n", x2);
    return 0;
}

3. Variadic macros

In GNU C, starting with C99, macros can accept a variable number of arguments, just like variadic functions. Like functions, macros also use three dots... to indicate variable parameters

VA_ARGS macro
The VA_ARGS macro is used to represent the content of variable parameters. Simply put, it is to copy the content of ... in the left macro to the position of __VA_ARGS__ on the right. The following example code:
 

#include <stdio.h>
#define debug(...) printf(__VA_ARGS__)
int main(void)
{
    int year = 2018;
    debug("this year is %d\n", year); //效果同printf("this year is %d\n", year);
}

4. Variable parameter alias
In addition, through some syntax, you can give a variable parameter a name instead of using __VA_ARGS__, such as args in the following example :

#include <stdio.h>
#define debug(format, args...) printf(format, args)
int main(void)
{
    int year = 2018;
    debug("this year is %d\n", year);  //效果同printf("this year is %d\n", year);
}

5. No parameter input


Different from the variable parameter function, the variable parameter in the variable parameter macro must have at least one parameter passed in, otherwise an error will be reported. In order to solve this problem, a special "##" operation is required. If the variable parameter is Ignored or empty, the "##" operator will cause the preprocessor to remove the preceding comma.

as shown in the example below

#include <stdio.h>
#define debug(format, args...) printf(format, ##args)
int main(void)
{
    int year = 2018;
    debug("hello, world");  //只有format参数,没有args可变参数
}

Guess you like

Origin blog.csdn.net/weixin_56819992/article/details/131114487