"C-Foundation" #define宏定义中的 #,##,@#,\ The magical uses of these symbols

Source: Internet
Author: User

The Magic usage of C + + macro commands.

First look at the following three statements:

#define CONN (x,y) x# #y
#define TOCHAR (x) #@x
#define TOSTRING (x) #x
1. # # Connection operator

# #表示连接 (token pasting, or token concatenation,merge two tokens into one while expanding macros). x# #y表示什么. Represents x connection Y, for example:

int n = Conn (123,456);
     ==> int n=123456;
char* str = Conn ("asdf", "ADF");
     ==> char* str = "ASDFADF";

How, it's amazing.

It should be noted that # #的左右符号必须能够组成一个有意义的符号, otherwise the preprocessor will complain. 2. #@ character operator

#@x can only be used in macro definitions that have incoming parameters, and must be placed before the parameter names in the macro definition body. The effect is to convert the passed single character parameter names into characters, and enclose them in a pair of simple quotes, which is to give X a single quotation mark, and return a const char.
For example,

char a = ToChar (1);
     ==> Char a= ' 1 ';

Do a trans-border test.

char a = ToChar (123);
     ==> Char a= ' 3 ';

But if your argument is more than four characters, the compiler will give you an error. Error C2015:too many characters in Constant:p 3. # string operator

#表示字符串化操作符 (stringification). The effect is to convert an incoming parameter name in a macro definition to a pair of double quotes around the parameter name string. It can only be used in macro definitions that have incoming parameters, and must be placed before the parameter names in the macro definition body. To put it bluntly, he was giving X double quotes:

char* str = ToString (123132);
     ==> char* str= "123132";

If you want to string the expanded macro parameters, you need to use a two-layer macro.

#define XSTR (s) str (s)
#define STR (s) #s
#define FOO 4
str (foo)
     ==> "foo"
xstr (foo)
     ==> XSTR (4)
     ==> str (4)
     ==> "4"

The s parameter is serialized in the STR macro, so it is not first expanded by the macro. The s parameter, however, is a generic parameter of the XSTR macro that has been expanded before being passed to the STR macro. 4. \ line to continue Operation

\ row continues when a defined macro cannot be fully expressed in one line, you can use the "\" (backslash) to indicate that the next line continues the definition of the macro.

Note: The last line does not add line characters.

VC preprocessor in the compiler will be automatically before and after the line to remove (write more lines, after the backslash can not have spaces, otherwise the compiler (arm or VC) will be an error. So as not to affect reading, but not logic, happy. 5. __va_args__

__VA_ARGS__ macros are used to accept an indefinite number of arguments. For example:

#define EPRINTF (...) fprintf (stderr, __va_args__)

eprintf ("%s:%d:", Input_file, Lineno) ==> fprintf  ( stderr, "%s:%d:", Input_file, Lineno)

When the __VA_ARGS__ macro precedes # #时, you can omit the parameter input. For example:

#define EPRINTF (format, ...) fprintf (stderr, format, # #__VA_ARGS__)

eprintf ("success!\n") ==> fprintf
    ( stderr, "success!\n");

Reference:
Macros

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.