代码定位:__FILE__, __FUNCTION__, __LINE__

来源:互联网 发布:淘宝号如何绑定支付宝 编辑:程序博客网 时间:2024/04/27 22:54
 

这是三个非常有用的全局变量,当程序需要输出一些内容,而又想知道输出的内容是在哪里输出的时候,这几个全局变量就派上用场了。
__FILE__,__FUNCTION__, __LINE__ 从名字可以直接看出来了,对应的:代码文件名, 函数名, 行号。

没啥好说的了,我不是写书的啊。

示例代码:
------------------------------------------------------------------------

//__FUNCTION__,__LINE__,__FILE__
//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

####################

可以看出,平时的日志之类的输出可以直接定位从哪个文件,哪个函数,哪行代码输出了哪些内容,极方便。