I. Introduction
The program runs in the background problems, need to see the detailed log, C language provides the trigger point log file name, line number, function name of the method, the key is to take advantage of the new C99 preprocessing identifier __VA_ARGS__; yourself with the following the compiler built-in macro definitions, macro definitions not only help us complete source code written in cross-platform, flexible use can subtly help us output is very useful debugging information.
Two, ANSI C standard macros
__LINE__ // insert in the source code of the current source code line number __FILE__ // insert the current source file name in the source file __DATE__ // insert the current date in the source file compiled __TIME__ // insert the current time to compile the source file __STDC__ / / when the program requires strict compliance with the ANSI C standard identifier is assigned 1 __cplusplus // this identifier is defined when you write a C ++ program _WIN32 // running bit 1 is defined in the windows system in linux // the program is running in linux bit 1 is defined on the system __x86_64__ // the program is run on a 64 bit system is defined as 1 __i386__ // program is run on a 32 bit system is defined as 1 __VA_ARGS__ in // is a variable parameter of the macro, the macro can be the new C99 is a new specification, //It seems currently supported (VC6.0 compiler does not support) after gcc and VC6.0. // macro role is preceded by ##, you can accept zero or more parameters
Third, examples
Macro instance:
#include <stdio.h> int main() { printf("__func__:%s\n", __func__); printf("__FILE__:%s\n", __FILE__); printf("__DATE__:%s\n", __DATE__); printf("__TIME__:%s\n", __TIME__); printf("__LINE__:%d\n", __LINE__); return 0; }
Examples of macro program output as follows:
__func__:main __FILE__:main.c __DATE__:Sep 14 2019 __TIME__:14:26:36 __LINE__:9
Four, # ##, and the operator
Where # and ## operators have different functions, but also here to do some introduction
# 1 for converting the parameters into a string
Example 1:
#define P(A) printf("%s:%d\n",#A,A); int main() { int a = 1, b = 2; P(a); P(b); P(a+b); return 0; }
Example 1 outputs the following procedure:
a:1 b:2 a+b:3
Example 2:
#define SQUARE(x) printf("The square of "#x" is %d.\n", ((x)*(x))) int main() { SQUARE(8); return 0; }
Example 2 procedure the following output:
The square of 8 is 64
2. The operator ## may be used to replace part of the macro function
#define XName (n-) n-X ## // If this use of macros: XName (. 8) // will be deployed in such a way: x8
## is a pressure-sensitive adhesive, before and after the two parts are bonded together, i.e. the "character" of the meaning. But "##" not at liberty to bond any character, must be a valid C language identifier. In a single macro definition, at most a "#" or "##" pretreatment operator may occur. If the "#" or "##" pretreatment relevant operator does not specify the order calculated, problems can occur. To avoid this problem, the use of only one operator (i.e., a "#" or "##", or do not) in a single macro definition. Unless it is necessary, or try not to use the "#" and "##."
__VA_ARGS__ is a variable parameter macro, few people know this macro, the macro variable parameter is the new norm in the new C99, now it seems that only supports gcc (VC6.0 compiler does not support). The last idea is to achieve a macro parameter defined in the parameter list for the ellipsis (ie three points).
## __ VA_ARGS__ macro role is preceded by ##, when the number of the variable parameter is 0, the front ## where the excess play "," effect removed, or otherwise a compile error; ## __ VA_ARGS__ examples:
#define my_print1 (FMT, ...) the printf (FMT, __VA_ARGS__ in) my_print1 ( " iiijjj \ n- " ) // Error printing my_print1 ( " I =% D, D J =% \ n- " , I, J) // print correctly #define my_print2 (fmt, ...) printf (fmt, ## __ VA_ARGS__) my_print2 ( " iiijjj \ the n- " ) // print correctly my_print2 ( " i =% d, J =% d \ the n- " , i, J) // print correctly