C language preprocessing for string and symbol pasting

Source: Internet
Author: User

In C language Development, macro definition is a very useful tool that makes our code easier to understand and easier to maintain. If a constant is used in many places, and may be modified according to different needs in the future, it is better to define it. In addition, the macro definition has other functions, understanding it, will better assist us in developing C programs. Let's look at an example:

#define CONN (x, y)

x# #y

#define TOSTRING (x)

#x

#define TOCHAR (x)

#@x

In these macro definitions, "# #", "#", and "#@" are used respectively for the following functions:

1, the bonding operator ##--connect two macro names, note that the connection is the macro name, not the value of its reference;

such as int Conn (A, B), defines an int type variable AB, which can be called directly AB, rather than the form of Conn (A, b);

printf (Conn ("AB", "CD"); the output is: ABCD

But:

#define M 0 #define VAR (x) var_# #x ... int var (M);//What is defined here?

According to the Ansi/iso c,# #操作符只是简单地粘接两个宏名, the int var (M) should be defined as var_m, which is not a problem, according to the test. However, in some older C-compiler environments, it is also possible to define VAR_0, such as testing in TC 2.0 found that "var_0=0" can be compiled, and "var_m=0" error.

2, the string operator #--the macro name into a string

such as printf (ToString (var1)); the output is: var1. VAR1 can be a defined variable name, or it can be a combination of characters that never appear

Similarly, if:

#define STR 0

...

printf (Tostring (STR));//

The current popular compilation environment will output STR, while TC 2.0 will output 0.

3. Character manipulation #@--convert macro names to characters, note: Earlier compilers may not support

Such as:

char c;

c = ToChar (1);//c = ' 1 '

c = ToChar (a);//c = ' a '

If the supplied macro has more than one character (note that no more than 4, or the compiler Error), the conversion result is the last character, such as

c = ToChar (ABC);//c = ' C '

c = ToChar (ABCD);//c= ' d '

c = ToChar (ABCDE);//error

To summarize, the usage is summed up, certainly not all.

1, the use of the following ANSI C in accordance with the provisions, but to remember that the compilation is not likely to be the early compiler does not support the C standard problem;

2, # #操作可应用在变量定义中, if the program development encountered to define a large number of variables, and these variables have the same prefix, # #很显得尤为重要, it can make the code more tidy, and reduce the probability of error. If you later find that you need to modify the variable prefix in bulk, you will be glad to use such a tool;

3, #操作符可用于调试时将变量名输出, with # #一起使用, such as define # define # Check_var (x,fmt) printf (#x "=" #fmt "\ n", x), then Check_var (var1,%d) is equivalent to printf (" var =%d\n ", var1);

Tips:

1. ANSI c stipulates that if the macro definition name appears in quotation marks ("or"), then no substitution is made, but some early compiler processing may be different, such as # define CTRL (C) (' C ' & 37), by the standard Ctrl (d) is expanded to (' C ' & 37). Obviously, this does not complete the author's intention, it happens to work under certain compilers is just an accident, should be avoided in practice.

2, the string connection does not have to use the # #这么麻烦, the actual two string constants can be directly written together, such as printf ("AB" "CD") output ABCD. or in use # #宏定义时, you can use printf (ToString (str) "\ n"), the output string after the line, not previously dared to use this, and later found that the test is relatively good, of course, directly with puts can also complete the same function.

3, to make # or # #转换的是宏字符常量的值也不是其名字, you can use indirect methods, such as:

#define TOSTRING (x) #x

#define XSTR (x) ToString (x)

#define STR1 STR2 ...

printf (Xstr (STR1));//output is STR2 rather than STR1


C language preprocessing for string and symbol pasting

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.