3078 views|4 replies

2015

Posts

0

Resources
The OP
 

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");

测试代码:

#include__LINE__); \
                            printf(__VA_ARGS__); \
                            printf("\n");

测试代码:

#include__LINE__); \
                            printf(__VA_ARGS__); \
                            printf("\n");

测试代码:

#include__LINE__); \
                            printf(__VA_ARGS__); \
                            printf("\n");

测试代码:

#include
#include

#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 DVR_PRT(format,...)  printf("[File:"__FILE__", Line:%d]  "format, __LINE__, ##__VA_ARGS__)

#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.

This post is from Microcontroller MCU

Latest reply

[attach]466383[/attach]   Details Published on 2020-3-25 11:18
 

1368

Posts

6

Resources
2
 
Nice, thanks for sharing!
This post is from Microcontroller MCU
 
Personal signature专注智能产品的研究与开发,专注于电子电路的生产与制造……QQ:2912615383,电子爱好者群: void
 

71

Posts

0

Resources
3
 
Thanks for sharing!
This post is from Microcontroller MCU
 
Personal signatureFTP
 
 

30

Posts

0

Resources
4
 
Nonsense, I just tried it and c51 doesn't support __VA_ARGS__
This post is from Microcontroller MCU
 
 
 

30

Posts

0

Resources
5
 

This post is from Microcontroller MCU
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list