c打印行号和函数
Sometimes, it is necessary to print some message on logic failure or anytime with the function name and line number, so that program can be debugged and fixed the issue.
有时,有必要在逻辑故障时或在任何时候使用功能名称和行号打印一些消息,以便可以调试程序并解决问题。
Prerequisite:
先决条件:
__LINE__: To print current line number
__LINE__ :打印当前行号
__func__: To print the name of the current function
__func__:打印当前函数的名称
Program:
程序:
#include <stdio.h>
float devide(int a, int b)
{
float result;
if(b!=0){
result = (float)a/(float)b;
}
else{
result=0;
printf("Error at line %d in function \"%s\"\n",((__LINE__)-3),__func__);
//ERROR at 3 lines above: ((__LINE__)-3)
}
return result;
}
int main()
{
printf("%f\n",devide(10,3));
printf("%f\n",devide(10,0));
return 0;
}
Output
输出量
3.333333
Error at line 9 in function "devide"
0.000000
c打印行号和函数