c/c++中#和##链接符号的用法
来源:互联网 发布:淘宝造物节官网 编辑:程序博客网 时间:2024/06/05 23:51
#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\n",__FUNCTION__,__LINE__,var); } int main() { unsigned int var; //# usage========= //参数展开为字符串 printf("str[%s]\n",FUNCTION_LIKE_MACRO( printf("str[hello!]\n");//这两条语句结果一样 //展开后"被转义\" printf("str[%s]\n",FUNCTION_LIKE_MACRO("hello!")); printf("str[\"hello!\"]\n"); //这个和英语原文描述的不一样,我理解错了吗? printf("str[%s]\n",FUNCTION_LIKE_MACRO(dir1\\sub0)); printf("str[dir1\\sub0]\n"); //前后空白字符删除 printf("str[%s]\n",FUNCTION_LIKE_MACRO( hello )); printf("str[hello]\n"); //中间件空白字符序列压缩成一个,字符串内除外 printf("str[%s]\n",FUNCTION_LIKE_MACRO(hello world "good morning")); printf("str[hello world \"good morning\"]\n"); //无效字符串,这个不明白,怎样验证 printf("str[%s]\n",FUNCTION_LIKE_MACRO(\a)); printf("str[\a]\n"); //##uase printf("concate number(123)=%d\n",CONCATE_NUMBER(123)); var=11111; // var += 22222; var CONCATE_ADDEQU(+,=) 22222; printf("var=%d\n",var); CONCATE_FUNC(myfun)(6890); return 0; }
0 0
- c/c++中#和##链接符号的用法
- C中“##”符号的用法
- C语言中宏定义"#"和"##"符号的用法
- C语言中符号常量的用法
- C/C++中&符号的用法
- C语言中有符号数和无符号数类型转换的用法深度解析
- c语言学习笔记(6)注释符号,接续符号和转义符号的用法
- "*" "::" "." ":" "->" c/c++编程中几种符号的用法和说明
- [C语言] 从C++中链接时的C符号的处理
- [c、c++]宏中"#"和"##"的用法
- [c、c++]宏中"#"和"##"的用法
- [C++]指针运算中关于*和&符号的运算实验
- C 语言中结构体引用符号"."和"->"的区别
- C C++ Java中链接器对全局符号的解析
- linux中硬链接和符号链接的区别
- Linux中硬链接和符号链接的区别
- linux中硬链接和符号链接的区别
- linux中硬链接和符号链接的区别
- android dip px sp
- windows7如何查看端口被占用
- 立志篇——开始阅读源码
- VS2008远程调试
- 黑马程序员_集合框架_泛型
- c/c++中#和##链接符号的用法
- Linux和windows之间资源共享--samba服务器配置
- Address already in use: JVM_Bind<null>:80 解决方案
- C语言 递归拆分数字
- JS获取中文拼音首字母,并通过拼音首字母快速查找页面内的中文内容
- MySQL之锁-1 降低锁竞争,减少MySQL用户等待时间
- apache运行CGI程序的配置
- poj1011(深搜+剪枝)
- 第十一周项目4-类族设计