GNU_C宏中的字符串替换#_##

来源:互联网 发布:解封淘宝店铺要多少钱 编辑:程序博客网 时间:2024/04/28 01:36

Stringification_#

Sometimes you may want to convert a macro argument into a stringconstant. Parameters are not replaced inside string constants, but youcan use the `#' preprocessing operator instead. When a macroparameter is used with a leading `#', the preprocessor replaces itwith the literal text of the actual argument, converted to a string constant.Unlike normal parameter replacement, the argument is not macro-expanded first. This is called stringification.

There is no way to combine an argument with surrounding text and stringify it all together. Instead, you can write a series of adjacent string constants and stringified arguments. The preprocessor willreplace the stringified arguments with string constants. The Ccompiler will then combine all the adjacent string constants into onelong string.

Here is an example of a macro definition that uses stringification:

     #define WARN_IF(EXP) \     do { if (EXP) \             fprintf (stderr, "Warning: " #EXP "\n"); } \     while (0)     WARN_IF (x == 0);          ==> do { if (x == 0)                fprintf (stderr, "Warning: " "x == 0" "\n"); } while (0);
The argument for EXP is substituted once, as-is, into theif statement, and once, stringified, into the argument tofprintf. If x were a macro, it would be expanded in the if statement, but not in the string.

The do and while (0) are a kludge to make it possible towriteWARN_IF (arg);, which the resemblance ofWARN_IF to a function would make C programmers want to do; seeSwallowing the Semicolon.

Stringification in C involves more than putting double-quote charactersaround the fragment. The preprocessor backslash-escapes the quotessurrounding embedded string constants, and all backslashes within string andcharacter constants, in order to get a valid C string constant with theproper contents. Thus, stringifying p = "foo\n"; results in"p = \"foo\\n\";". However, backslashes that are not inside stringor character constants are not duplicated: `\n' by itselfstringifies to "\n".

All leading and trailing whitespace in text being stringified isignored. Any sequence of whitespace in the middle of the text isconverted to a single space in the stringified result. Comments arereplaced by whitespace long before stringification happens, so theynever appear in stringified text.

There is no way to convert a macro argument into a character constant.

If you want to stringify the result of expansion of a macro argument,you have to use two levels of macros.

     #define xstr(s) str(s)     #define str(s) #s     #define foo 4     str (foo)          ==> "foo"     xstr (foo)          ==> xstr (4)          ==> str (4)          ==> "4"
s is stringified when it is used in str, so it is notmacro-expanded first. Buts is an ordinary argument toxstr, so it is completely macro-expanded beforexstritself is expanded (see Argument Prescan). Therefore, by the timestr gets to its argument, it has already been macro-expanded.

Concatenation_##

It is often useful to merge two tokens into one while expanding macros. This is calledtoken pasting ortoken concatenation. The`##' preprocessing operator performs token pasting. When a macro is expanded, the two tokens on either side of each `##' operatorare combined into a single token, which then replaces the `##' and the two original tokens in the macro expansion. Usually both will b eidentifiers, or one will be an identifier and the other a preprocessing number. When pasted, they make a longer identifier. This isn't theonly valid case. It is also possible to concatenate two numbers (or anumber and a name, such as1.5 ande3) into a number. Also, multi-character operators such as+= can be formed bytoken pasting.

However, two tokens that don't together form a valid token cannot bepasted together. For example, you cannot concatenatex with+ in either order. If you try, the preprocessor issues a warningand emits the two tokens. Whether it puts white space between thetokens is undefined. It is common to find unnecessary uses of `##'in complex macros. If you get this warning, it is likely that you cansimply remove the `##'.

Both the tokens combined by `##' could come from the macro body,but you could just as well write them as one token in the first place.Token pasting is most useful when one or both of the tokens comes from amacro argument. If either of the tokens next to an `##' is aparameter name, it is replaced by its actual argument before `##'executes. As with stringification, the actual argument is notmacro-expanded first. If the argument is empty, that `##' has noeffect.

Keep in mind that the C preprocessor converts comments to whitespace before macros are even considered. Therefore, you cannot create acomment by concatenating `/' and `*'. You can put as muchwhitespace between `##' and its operands as you like, includingcomments, and you can put comments in arguments that will beconcatenated. However, it is an error if `##' appears at eitherend of a macro body.

Consider a C program that interprets named commands. There probably needs to be a table of commands, perhaps an array of structures declaredas follows:

     struct command     {       char *name;       void (*function) (void);     };          struct command commands[] =     {       { "quit", quit_command },       { "help", help_command },       ...     };
It would be cleaner not to have to give each command name twice, once inthe string constant and once in the function name. A macro which takes thename of a command as an argument can make this unnecessary. The stringconstant can be created with stringification, and the function name byconcatenating the argument with `_command'. Here is how it is done:

     #define COMMAND(NAME)  { #NAME, NAME ## _command }          struct command commands[] =     {       COMMAND (quit),       COMMAND (help),       ...     };

http://gcc.gnu.org/onlinedocs/cpp/Stringification.html#Stringification

http://gcc.gnu.org/onlinedocs/cpp/Concatenation.html#Concatenation


原创粉丝点击