C/C++魔法宏

C/C++魔法宏

1 魔法宏

  • __LINE__ : 代表该行代码的所在行号;
  • __FILE__ : 代表源文件的文件名;
  • __DATE__ : 代表该源文件被编译的(月 日 年)日期格式;
  • __TIME__ : 代表该源文件被编译的(时:分:秒)时间格式;
  • __FUNCTION__ : 代表该源代码中插入当前所在函数名称;
  • __STDC__ : 当该程序严格遵循ANSI C标准时该标识被赋值为1;
  • __cplusplus:当编写的C++程序时该标识符被定义。

2 示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main(int argc, char *argv[])
{
std::cout<<__LINE__<<std::endl;
std::cout<<__FILE__<<std::endl;
std::cout<<__DATE__<<std::endl;
std::cout<<__TIME__<<std::endl;
std::cout<<__FUNCTION__<<std::endl;
std::cout<<__STDC__<<std::endl;
std::cout<<__cplusplus<<std::endl;

return 0;
}

3 输出

1
2
3
4
5
6
7
5
..\Test\main.cpp
Jan 17 2019
22:37:28
main
1
201103