Duanxx C ++: Use _ FILE __,__ LINE __,__ VA_ARGS _ to debug the code, duanxx _ file __
_ FILE __,__ LINE __
What we want to know most when debugging C/C ++ code is, of course, the number of lines of the file to which the program is currently going. There are two macros: __file _ and _ LINE __, return the path of the current FILE and the current row.
The test code is as follows:
#include <cstdio>int main(){ printf("%s : %d",__FILE__,__LINE__); printf("\n"); return 0;}
The running result in Windows is:
The running result of Linux is as follows:
In Windows, __file _ returnsFull pathIn Linux, if g ++ is used for compiling, onlyFile Name.
_ VA_ARGS __
This is a variable parameter macro supported since C99. Previously, in C language, only variable parameters of functions are supported.
For example, the most common printf function:
void printf(const char* format, …);
Ellipsis ..., YesVariable parameters.
The macro _ VA_ARGS _ is a variable parameter that implements the macro:
#define debug(…) printf(__VA_ARGS__)
The test code is as follows:
#include <cstdio>#define DEBUG(...) printf(__VA_ARGS__)int main(){ DEBUG("duanxx %d\n",123); return 0;}
Running results in Windows:
Running result in Linux:
One row debugging macro
With the above description, you can write the following debugging macro:
#define DEBUG(fmt, ...) \ fprintf(stdout, "%s:%d Duanxx Debug " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
Test code:
#include <cstdio>#define DEBUG(fmt, ...) \ fprintf(stdout, "%s:%d Duanxx Debug " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)int main(){ DEBUG("duanxx %d\n",123); return 0;}
Windows results:
Result in Linux:
In C, how does one use _ FILE _ and _ LINE _?
Note: It is a double underline, not a single underline.
_ FILE _ string containing the current program FILE name
_ LINE _ indicates the integer of the current row number.
_ DATE _ string containing the current DATE
_ STDC _ If the compiler complies with the ansi c standard, it is a non-zero value.
_ TIME _ string containing the current TIME
For example:
Program that outputs the current program file name:
//---------------------------------------------------------------------------
# Include <stdio. h>
Int main (void)
{
Printf ("% s" ,__ FILE __);
Return 0;
}
//---------------------------------------------------------------------------
C language _ VA_ARGS _
_ VA_ARGS _ is to directly convert the... in brackets into the actual string. USE_FEATURE_REMOTE_LOG (OPTBIT_remote,) is equal to OPTBIT_remote.
Your printf error occurs. Check whether the position of your printf is not defined by OPTBIT_remote.