A lot of things have not been remembered, think of a little bit of writing, hit a bit to write a little, slowly accumulate.
On
#在宏定义中用于Replace the character passed in the variable , for example: #define WHOLE_OPERATION (n) do {printf (#n "=%d\n", (n));} while (0);
Calling Whole_operation (5*6), output: 5*6=30, helps increase the readability of the output.
About # #
# #是c99中定义的用于Glue two symbols , identifiers or parameters. Example: #define NAME_INDEX (index) name_# #index
Call Name_index (1) to generate the name_1 variable, so many times # #用于动态的调用标识符具有一定规律的函数, macro or variable.
For example, if you now have add_arg_1 (), add_arg_2 () two functions, only at run time to know which function to call, then you can use the following code:
#define CALL_ADD_ARG (ARGC) add_arg_# #argc ()
The Ipnet log module uses ipcom_log_# #x的方式在运行是判断需要输出什么级别的log because the log level is different and the first few identifiers of the log level are ipcom_log_.
About indeterminate parameters ...
A long time ago on the fire wall to do the log module, the use of a more interesting trick. Because log to receive different information of different modules, but each module contains its own unique information, in order to ensure that all information can be accepted by the log, at that time using an indefinite function as log interface log (MsgId, ...), through the va_list AP; Va_start (AP, Firstarg); Va_arg (AP, type); Va_end (AP); The way to accept incoming arguments. In fact, most prinf are implemented in this way. However, the function call in this way is prone to problems, and the function does not know when the parameter ends and it may cause the program to crash.
C99 defines the macro that __VA_ARGS__ uses to accept indeterminate parameters: #define LOG (MsgId, ...) Log (MsgId, __va_args__, Lastarg) or # define LOG (MsgId, arg ...) The log (MsgId, ARG, Lastarg) Lastarg is a predefined macro or variable used to identify the end, and if not __va_args__, arg ... To replace. In this way, the log function can accept as many as 1 or more parameters without needing a relationship when it ends. So if log is called with only one msgid parameter, it will become log (MSGID,,LASTARG), this time need to use # #的另一个作用, If # #, there are no parameters, then the comma will be omitted , so the final definition of the log function becomes: #define LOG (MsgId, ...) Log (MsgId, # #__VA_ARGS__, Lastarg)
Some trick about C language