版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/88651126
一 标准宏的例子
1 代码
#include <iostream>
using namespace std;
// 标准宏名称
#include <iostream>
using namespace std;
int main()
{
cout << "This is the line number "
<< __LINE__;
cout << " of file " << __FILE__
<< ".\n";
cout << "Its compilation began "
<< __DATE__;
cout << " at " << __TIME__ << ".\n";
cout << "The compiler gives a "
<< "__cplusplus value of "
<< __cplusplus;
cout << endl;
return 0;
}
2 运行
[root@localhost test]# g++ test.cpp -g -o test
[root@localhost test]# ./test
This is the line number 10 of file test.cpp.
Its compilation began Mar 18 2019 at 21:40:16.
The compiler gives a __cplusplus value of 199711
二 使用宏__func__得到函数名
1 代码
#include<stdio.h>
const char *test()
{
return __func__;
}
int main ()
{
printf("%s,%s",__func__,test());
return 0;
}
2 运行
[root@localhost test]# g++ test1.cpp -g -o test1
[root@localhost test]# ./test1
main,test
三 变长参数的宏
1 代码
#include<stdio.h>
#define DBGDUMP(...) \
{ \
printf("%s\n%s,%d\n",__FILE__,__func__,__LINE__); \
printf(__VA_ARGS__); \
}
int main ()
{
int ret = 5;
DBGDUMP("ret=%d\n",ret);
return 0;
}
2 运行
[root@localhost test]# g++ test1.cpp -g -o test1
[root@localhost test]# ./test1
test1.cpp
main,10
ret=5