返回> 网站首页 

[转载]代码定位:__FILE__ , __FUNCTION__ , __LINE__

yoours2015-06-04 12:11:16 阅读 1753

简介一边听听音乐,一边写写文章。

当程序需要输出一些内容,而又想知道输出的内容是在哪里输出的时候,这几个全局变量就派上用场了。
__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
可以看出,平时的日志之类的输出可以直接定位从哪个文件,哪个函数,哪行代码输出了哪些内容,极方便。

微信小程序扫码登陆

文章评论

1753人参与,0条评论