C language Macro Definition # #连接符和 use of the # symbol (MFC is the # #自动把消息和消息函数对应起来了, with macros to reduce the amount of switch case code written)

Source: Internet
Author: User
Tags switch case

C languageThe use of macros (macro) in macro C (and C + +) belongs to the scope of compiler preprocessing, which is a compile-time concept (not a run-time concept). The following is a simple summary of the use of macros encountered. About # and # #

In a C-language macro, #的功能是将其后面的宏参数进行字符串化操作 (stringfication) is simply to add a double quotation mark to the left and right of the macro variable that it refers to by replacing it. For example, a macro in the following code:

#define WARN_IF (exp)    do{IF (exp)    fprintf (stderr, "Warning:" #EXP "/n");}   while (0)

Then the replacement procedure shown in the actual use will appear as follows:

warn_if (divider = = 0);
is replaced by
do {
if (divider = = 0)
fprintf (stderr, "Warning" "divider = = 0" "/n");
} while (0);

This way, each time the divider (divisor) is 0, a message is printed on the standard error stream.

and # #被称为连接符 (concatenator), used to connect two tokens to a token. Note that the object connected here is token, not necessarily a macro variable. For example, you want to make a menu item command name and function pointer composition of the structure of the array, and you want to have the function name and menu item command name has an intuitive, name relationship. Then the following code is very useful:

struct command
{
char * name;
void (*function) (void);
};
#define COMMAND (name) {name, name # # _command}
Then you can easily initialize an array of command structures with some pre-defined commands:
struct command commands[] = {
COMMAND (quit),
COMMAND (Help),
...
}

The command macro acts as a code generator here, which reduces the code density to a certain extent, and indirectly reduces the errors caused by careless attention. We can also N # #符号连接 n+1 A token, this feature is not available in the # symbol. Like what:

#define Link_multiple (a,b,c,d) a# #_ # #b # #_ # #c # #_ # #d
typedef struct _RECORD_TYPE link_multiple (name,company,position,salary);
Here this statement will be expanded to:
typedef struct _RECORD_TYPE name_company_position_salary;


http://blog.csdn.net/dotphoenix/article/details/4345174
Http://www.cnblogs.com/xuxm2007/archive/2011/06/10/2077255.html
https://my.oschina.net/shelllife/blog/123202

C language Macro definition # #连接符和 # symbol use (MFC is the # #自动把消息和消息函数对应起来了, with macros to reduce the amount of switch case code written)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.