´®¿Ú¸ñʽ»¯Êä³ö³ýÁË¿ÉÒÔͨ¹ýÖض¨Òåfputcº¯ÊýʵÏÖ£¬»¹¿ÉÒÔ×Ô¼º¶¨Òå±ä²ÎÊýº¯ÊýʵÏÖ¡£
±¾ÎÄÕÂÖصãÃèÊöÈçºÎʹÓÃ×Ô¼º¶¨ÒåµÄ±ä²ÎÊýº¯ÊýʵÏÖ´®¿ÚµÄ¸ñʽ»¯Êä³ö¡£
1.±ä²ÎÊý£ºÔÚCÖÐÈç¹ûÒª¶¨ÒåµÄº¯Êý°üº¬µÄ²ÎÊý¸öÊý²»È·¶¨£¬¿ÉÒÔÀûÓá°...¡±£¬Èý¸öµãÀ´¶¨Ò岻ȷ¶¨³¤¶ÈµÄ²ÎÊý¡£
2.¸ñʽ»¯¸ñʽ»¯Êä³ö£ºÒòΪÎÒÃÇҪʹÓô®¿Ú¸ñʽ»¯Êä³ö£¬ËùÒÔÎÒÃǶ¨ÒåµÄ±ä²ÎÊýº¯Êý×îºÃÊǺÍprintfÒ»ÑùµÄÐÎʽ
±ÈÈç void MyPrintf£¨const char *fmt£¬...£©¡£
3. va_list£¬ va_start ( va_list ap, prev_param )£¬ºÍva_end ( va_list ap )Ó÷¨¡£
va_list ÓÃÀ´¶¨ÒåÒ»¸ö²ÎÊýÖ¸Õ룬±ÈÈçva_list ap£»ÔòapÊÇÖ¸Ïò²ÎÊýµÄÖ¸Õ룬Æä±¾ÖÊÊÇchar* ÐÍÖ¸Õë¡£
va_start(va_list ap, prev_param),½«apÖ¸Ïòprev_param£¬×÷ÓÃÊǽ«apÖ¸Ïò²ÎÊýÁбíµÄµÚÒ»¸ö²ÎÊý£¬Õâ¸ö²ÎÊýÊÇ
´Ó×ó±ßÆðµÄµÚÒ»¸ö£¬¸ÕºÃºÍÈëÕ»µÄ·½ÏòÏà·´¡£Ò²¾ÍÊÇ×îºóÒ»¸öÈëÕ»µÄ²ÎÊý¡£
va_end ( va_list ap )ºÜ¼òµ¥¾ÍÊǶªÆúapÖ¸Õ룬ÁîËûÖ¸Ïò¿Õ¡£
4.vsprintf(char* str1,const char* str2,va_list)£ºÓ÷¨ºÍsprintfÏàËÆ£¬×÷ÓÃÊǽ«str2µÄÄÚÈݸ´ÖƵ½str1£¬ÆäÖдøÓеĸñʽ»¯²ÎÊý£¬Èë%d£¬%x£¬%cµÈµÈ
ÓɺóÃæµÄ²ÎÊýÖ¸Õë va_list Ö¸ÏòµÄ²ÎÊý´úÌæ¡£¶øÎÒÃÇÖ»Òª½«¸ñʽ»¯×Ö·û´®µÄÊ×µØÖ·¸østr2¾Í¿ÉÒÔÁË¡£ÕâÑù×îºóÎÒÃÇÀûÓô®¿ÚµÄ·¢ËÍ×Ö·û´®º¯ÊýÊä³ö×Ö·û
´®str1¾Í¿ÉÒÔ´ïµ½¸ñʽ»¯Êä³öµÄÄ¿µÄÁË¡£
¾ßÌåʵÏÖ´úÂëÈçÏ£º
#include "stdarg.h" void MyPrintf(const char *fmt,...) { va_list ap; va_start(ap,fmt); vsprintf((char*)g_uart1_tx_buff,fmt,ap); va_end(ap); uart1_sendstr(g_uart1_tx_buff); } |