#include <stdio.h> #include <stdlib.h> /* 英语原文: In function-like macros, a # operator before an identifier in the replacement-list runs the identifier through parameter replacement and encloses the result in quotes, effectively creating a string literal. In addition, the preprocessor adds backslashes to escape the quotes surrounding embedded string literals, if any, and doubles the backslashes within the string as necessary. All leading and trailing whitespace is removed, and any sequence of whitespace in the middle of the text (but not inside embedded string literals) is collapsed to a single space. This operation is called "stringification". If the result of stringification is not a valid string literal, the behavior is undefined. */ /*翻译 在类函数宏中,替换参数标示符前加#,展开结果实际上是替换参数加双引号的字符串. 另外,如果参数中有双引号,预处理器替换后会在双引号前加反斜杠,转义字符。 如果有反斜杠,对反斜杠加反斜杠转义。 前置后置空白字符删除,中间连续多个空白字符压缩成一个(字符串除外)。 这样的操作被称为"字符串化"。假如结果不是有效字符串化,行为没有被定义。 */ /*结论: 1.#在宏替换中产生"参数"这样的字符串 2.参数中"被展开成" 3.参数中被展开成\ 4.参数前置后置空白字符展开时被删除 5.参数中间的空白序列展开时变成一个,参数为双引号引起来的字符串除外 6.展开后是无效字符串,行为没有被定义,不同的预处理器有不同的处理方式 */ #define FUNCTION_LIKE_MACRO(params) (#params) /* 英语原文: A ## operator between any two successive identifiers in the replacement-list runs parameter replacement on the two identifiers and then concatenates the result. This operation is called "concatenation" or "token pasting". Only tokens that form a valid token together may be pasted: identifiers that form a longer identifier, digits that form a number, or operators + and = that form a +=. A comment cannot be created by pasting / and * because comments are removed from text before macro substitution is considered. If the result of concatenation is not a valid token, the behavior is undefined. */ /* 翻译: ##操作符在两个连续的标示符之间,当宏代替时,结果链接这两个标示符. 这种操作被称作"链接"或者"符号黏贴". 只有能形成有效的标示符的符号才能黏贴在一起:标示符形成一个长标示符;数字形成数,+和=形成+=. /和*不能形成注释,因为预处理器在宏代替之前都有会把注释去掉。 假如链接不是有效的符号,这种行为没有定义。 */ /* 1.##链接标示符形成一个标示符 2.只有形成有效的标示符才能链接 3.链接结果是无效符号怎样验证 */ #define CONCATE_NUMBER(param) (100##param) #define CONCATE_ADDEQU(add,equ) add##equ #define CONCATE_FUNC(name) FUNC_##name //打印函数名字,使用##举例 void CONCATE_FUNC(myfun)(int var) { printf("%s[%d]var=%d ",__FUNCTION__,__LINE__,var); } int main() { unsigned int var; //# usage========= //参数展开为字符串 printf("str[%s] ",FUNCTION_LIKE_MACRO( printf("str[hello!] ");//这两条语句结果一样 //展开后"被转义" printf("str[%s] ",FUNCTION_LIKE_MACRO("hello!")); printf("str["hello!"] "); //这个和英语原文描述的不一样,我理解错了吗? printf("str[%s] ",FUNCTION_LIKE_MACRO(dir1\sub0)); printf("str[dir1\sub0] "); //前后空白字符删除 printf("str[%s] ",FUNCTION_LIKE_MACRO( hello )); printf("str[hello] "); //中间件空白字符序列压缩成一个,字符串内除外 printf("str[%s] ",FUNCTION_LIKE_MACRO(hello world "good morning")); printf("str[hello world "good morning"] "); //无效字符串,这个不明白,怎样验证 printf("str[%s] ",FUNCTION_LIKE_MACRO(a)); printf("str[a] "); //##uase printf("concate number(123)=%d ",CONCATE_NUMBER(123)); var=11111; // var += 22222; var CONCATE_ADDEQU(+,=) 22222; printf("var=%d ",var); CONCATE_FUNC(myfun)(6890); return 0; }