C语言中_FILE__用以指示本行语句所在源文件的文件名,__LINE__用以指示本行语句在源文件中的位置信息
可定义函数宏实现打印错误位置信息
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#define EOL "\r\n"
#else
#define EOL "\n"
#endif
#define err(x) fprintf(stderr, EOL "[%s:%i] Fatal error: %s" EOL, __FILE__, __LINE__, x);abort();
使用
int main(){
int i = 0;
if(0 == i){
err("i should not be 0");//line 15
}
return 0;
}
输出
gcc test.c -o a.exe
a.exe
[test.c:15] Fatal error: i should not be 0