Definition of variables "#X", "x##", "# #X" and "# #X # #" in C language

Source: Internet
Author: User

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:

1. #使用

#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); where divider = = 0

is replaced by

 Do {  if0)   "Warning " " Divider = = 0""/N"while (0);

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

2. # #使用

The # # is called a connector (Concatenator), which is 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} Here's # #表示后面有内容与NAME链接

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//Note the rules here (a# #开头, # #d结束, in the Middle # #x # #)

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;

Personally think # #类似 operator + acts as a connection.

3 See again One # use as follows:

#define int Main () {   display (name);}


The running result is name, why is it not "#name"?
---------------------------------------------------------------

#在这里是字符串化的意思
printf ("#name") is equivalent to
printf ("" "Name" "")

Definition of variables "#X", "x##", "# #X" and "# #X # #" in C language

Related Article

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.