C/C++ 宏中#和##

来源:互联网 发布:网络有好有坏的英文 编辑:程序博客网 时间:2024/06/07 19:04

C/C++ 宏中#和##

1、宏中的#

  C/C++宏中#将宏参数 字符串化。例如:

#define TOSTR(s) #schar* str = TOSTR(hello world); // str = "hello world"std::cout<<str <<'\n';output : hello world

2、宏中的##

  ##对文本连接。例如:

#define TEST(m, n) m##n#define S_CONN(m, n) m##.##nint dData = TEST(3,4);          //34double fData = S_CONN(3,4);     //3.4std::cout<<"dData:" <<dData         <<", fData:"<<fData <<'\n';output : dData:34, fData:3.4

3、宏参数本身为宏

  宏中有#和##时,若参数是宏,该参数宏不会展开。若要展开,需要加一个中间宏进行转换。例如:

#define _TOSTR(s) #s#define TOSTR(s) _TOSTR(s)std::cout<< "_TOSTR(_S_CONN(8, 4)): "<< _TOSTR(_S_CONN(8, 4))<<"\n"            <<"TOSTR(_S_CONN(8, 4)): "<< TOSTR(_S_CONN(8, 4))<<'\n';output : _TOSTR(_S_CONN(8, 4)) :  _S_CONN(8, 4)            TOSTR(_S_CONN(8, 4)) :  8.4
0 0
原创粉丝点击