c/c++中#号的用法
2014-09-25 14:44
239 查看
转载:http://blog.sina.com.cn/s/blog_4cf015f201009o1d.html
第一种情况作为Stringizing Operator(#),暂叫字符串化吧。
#define stringer( x ) printf( #x "\n" )
int main()
{
stringer( In quotes in the printf function call\n );
stringer( "In quotes when printed to the screen"\n );
stringer( "This: \" prints an escaped double quote" );
}
如此的定义在进行处理时将被转换为如下代码:
int main()
{
printf( "In quotes in the printf function call\n" "\n" );
printf( "\"In quotes when printed to the screen\"\n" "\n" );
printf( "\"This: \\\" prints an escaped double quote\"" "\n" );
}
程序运行结果如下:
In quotes in the printf function call
"In quotes when printed to the screen"
"This: \" prints an escaped double quotation mark"
第二种情况作为Charizing Operator(#@),就叫字符化吧。
例子:
#define makechar(x) #@x
causes the statement
a = makechar(b);
这条语句将被扩展为
a = 'b';
注意单引号字符不能使用这样的字符化。
第三种情况作为
Token-Pasting Opertor(##),叫做连接符吧,大概是这样的意思。
例子:#define paster( n ) printf( "token" #n " = %d", token##n ) int token9 = 9;
如果一个数字作为此宏的参数调用的话,
paster( 9 );
宏被展开为
printf( "token" "9" " = %d", token9 );
进而成为这样的语句:printf( "token9 = %d", token9 );
参考链接:http://blog.sina.com.cn/s/blog_4cf015f201009o1d.html
第一种情况作为Stringizing Operator(#),暂叫字符串化吧。
#define stringer( x ) printf( #x "\n" )
int main()
{
stringer( In quotes in the printf function call\n );
stringer( "In quotes when printed to the screen"\n );
stringer( "This: \" prints an escaped double quote" );
}
如此的定义在进行处理时将被转换为如下代码:
int main()
{
printf( "In quotes in the printf function call\n" "\n" );
printf( "\"In quotes when printed to the screen\"\n" "\n" );
printf( "\"This: \\\" prints an escaped double quote\"" "\n" );
}
程序运行结果如下:
In quotes in the printf function call
"In quotes when printed to the screen"
"This: \" prints an escaped double quotation mark"
第二种情况作为Charizing Operator(#@),就叫字符化吧。
例子:
#define makechar(x) #@x
causes the statement
a = makechar(b);
这条语句将被扩展为
a = 'b';
注意单引号字符不能使用这样的字符化。
第三种情况作为
Token-Pasting Opertor(##),叫做连接符吧,大概是这样的意思。
例子:#define paster( n ) printf( "token" #n " = %d", token##n ) int token9 = 9;
如果一个数字作为此宏的参数调用的话,
paster( 9 );
宏被展开为
printf( "token" "9" " = %d", token9 );
进而成为这样的语句:printf( "token9 = %d", token9 );
参考链接:http://blog.sina.com.cn/s/blog_4cf015f201009o1d.html
相关文章推荐
- c/c++中#号的用法
- as3 下使用include,类似,c/c++用法;前面不用#号,demo
- c/c++中#号的用法(转)
- c++ volatile用法(转)
- C++中const 的用法
- C++的命名参数惯用法(Named Parameter Idiom)
- C++中const用法浅析
- [C++语法] 关键字typedef用法(转)
- C++风格的类型转换的用法
- C++回调函数 用法
- (转)关于C++中函数指针的使用(包含对typedef用法的讨论)
- C/C++语言中可变参数的用法
- C++中分层的用法
- c++中const的用法详解
- 关于C++中函数指针的使用(包含对typedef用法的讨论)
- 关于C++中函数指针的使用(包含对typedef用法的讨论)
- 关于C++中函数指针的使用(包含对typedef用法的讨论)
- C++ 引用用法
- 关于C++中函数指针的使用(包含对typedef用法的讨论)
- 关于C的变长参数的思考心得/C/C++语言中可变参数的用法