Linux/Unix C 编程 之(四)代码定位:__FILE__, __FUNCTION_, __LINE__
代码定位:__FILE__, __FUNCTION__, __LINE__
这是三个非常有用的全局变量,当程序需要输出一些内容,而又想知道输出的内容是在哪里输出的时候,这几个全局变量就派上用场了。
__FILE__,__FUNCTION__, __LINE__ 从名字可以直接看出来了,对应的:代码文件名, 函数名, 行号。
示例代码:
------------------------------------------------------------------------
//testout.c
#include <stdio.h>
#include <stdlib.h>
void testout()
{
printf("cur func : %s ; cur line : %d\n", __FUNCTION__, __LINE__);
return;
}
void main()
{
printf("cur file : %s ; cur func : %s ; cur line : %d\n", __FILE__, __FUNCTION__, __LINE__);
testout();
return;
}
------------------------------------------------------------------------
####################
编译:
gcc -o testout testout.c
运行:
./testout
输出:
cur file : testout.c ; cur func : main ; cur line : 15
cur func : main ; cur line : 9
####################