ANSI C标准中有几个标准预定义宏:
__LINE__:在源代码中插入当前源代码行号;
__FILE__:在源文件中插入当前源文件名;
__DATE__:在源文件中插入当前的编译日期
__TIME__:在源文件中插入当前编译时间;
__func__: 当前所在函数名,在编译器的较高版本中支持 __FUNCTION__: 当前所在函数名
使用实例
#include <stdio.h>
#include <stdlib.h>
void why_me();
int main()
{
printf( "The file is %s.\n", __FILE__ );
printf( "The date is %s.\n", __DATE__ );
printf( "The time is %s.\n", __TIME__ );
printf( "This is line %d.\n", __LINE__ );
printf( "This function is %s.\n", __func__ );
why_me();
return 0;
}
void why_me()
{
printf( "This function is %s\n", __func__ );
printf( "The file is %s.\n", __FILE__ );
printf( "This is line %d.\n", __LINE__ );
}
运行结果
The file is debug.c.
The date is Jun 6 2012.
The time is 09:36:28.
This is line 11.
This function is main.
This function is why_me
The file is debug.c.
This is line 22.