Preprocessor "#" 用法

Preprocessor "#" 用法

Formal parameters are not replaced within quoted strings. If, however, a parameter name is preceded by a # in the replacement text, the combination will be expanded into a quoted string with the parameter replaced by the actual argument. This can be combined with string concatenation to make, for example, a debugging print macro:

   #define  dprint(expr)   printf(#expr " = %gn", expr)

When this is invoked, as in

   dprint(x/y)

the macro is expanded into

   printf("x/y" " = &gn", x/y);

and the strings are concatenated, so the effect is

   printf("x/y = &gn", x/y);

Within the actual argument, each " is replaced by " and each by \, so the result is a legal string constant.

The preprocessor operator ## provides a way to concatenate actual arguments during macro expansion. If a parameter in the replacement text is adjacent to a ##, the parameter is replaced by the actual argument, the ## and surrounding white space are removed, and the result is re-scanned. For example, the macro paste concatenates its two arguments:

   #define  paste(front, back)  front ## back

so paste(name, 1) creates the token name1.

Leave a Reply

Your email address will not be published. Required fields are marked *