C51 uses macro definition to replace printf function[Copy link]
Problem Sometimes we want to use macro definitions to decide whether to compile debug version code or release version code. The debug version code will print debugging information through printf, but the release version code will not. We can't write every printf like this: #if _DEBUG_ printf("hello world!"); #endif This is really too troublesome! If you need to print everywhere, it will make the layout look very messy. GCC compiler solution Later I thought of a method. If the compiler is GCC, macro definition can be used instead of printf function. Since printf is a function with variable parameters, variable parameter macros (… and __VA_ARGS__) are used here. Write this code under the header file #define _DEBUG_ 1 #if _DEBUG_ #define PR(...) printf(__VA_ARGS__) #else #define PR(...) #endif When you need to print debugging information later, just use the PR macro. If you need a release version and do not print debugging information, set DEBUG to 0 and the compiled program will not print debugging information. Problems in keil C51 The gcc compiler and c51 are two different compilers, so the standards for C language compilation are also different. If C51 uses GCC to compile standard macros to replace printf functions, your code will report an error. The cache memory of C51 is limited, and macro definitions do not allow functions with indefinite parameters. I thought about it for a long time. It does not define indefinite parameter functions for macros, but can use printf indefinite parameter functions. Can I skip the indefinite parameter functions? #define _debug_ 1 #if _debug_ #define debug_printbackf printf[ /size] I cleverly used the define replacement feature. If _debug_ is equal to 1, debug_printf is equal to printf, and the output is normal. However, when _debug_ is equal to 0, printing will be turned off, debug_printf will be equal to //, and the subsequent print will be commented out during compilation. In the actual debugging process, the most basic debugging method printf is often used, but it is often necessary to use parameters __FILENAME__, __FUNCTION__, and __LINE__ in the print function. Especially in large projects, it feels a bit cumbersome to repeatedly write these parameters during coding, so it is natural to think of macro definitions. Of course, you can also refer to the printf function to write your own My_Printf function, but if you don’t want to go through the trouble, just use macro definitions! The code is as follows: Environment standard C99, GNC [ /color] #define PRT(...) printf("Filename %s, Function %s, Line %d > ", __FILE__, __FUNCTION__, __LINE__); \ printf(__VA_ARGS__ ); \ printf("\n"); Test code: [ color=#000000] #include__LINE__); \
printf(__VA_ARGS__); \
printf("\n");
#define PRT(...) printf("Filename %s, Function %s, Line %d > ", __FILE__, __FUNCTION__, __LINE__); \
printf(__VA_ARGS__); \
printf("\n");
int main()
{
int a =0;
PRT("a");
PRT("hello, %d ", 10);
PRT("%d, %s, %d", 10, "dafdsa", 20);
return 0;
}
测试结果:
root@ubuntu:/home/ybq/Desktop# gcc printf.c -o printf
root@ubuntu:/home/ybq/Desktop# ./printf
Filename printf.c, Function main, Line 22 > a
Filename printf.c, Function main, Line 23 > hello, 10
Filename printf.c, Function main, Line 24 > 10, dafdsa, 20
#define PRT(format,...) printf("[File:%s, Line:%d] "format, __FILE__, __LINE__,##__VA_ARGS__) This technique can be used in single-chip C language development, and it is very convenient to switch versions. How to redirect printf to the serial port in the keil environment, you can refer to here.