代碼測試----不定參函數

  1. #include <stdio.h>  
  2. #include <stdarg.h>  
  3.   
  4. void MyPrintf(char *format, ...)  
  5. {  
  6.     va_list ap;  
  7.     va_start(ap, format);  
  8.     vprintf(format, ap);  
  9.     va_end(ap);  
  10. }  
  11.   
  12. void Error(char *format, ...)  
  13. {  
  14.     va_list ap;  
  15.     va_start(ap, format);  
  16.     fprintf(stdout, "err:");  
  17.     vprintf(format, ap);  
  18.     va_end(ap);  
  19. }  
  20.   
  21. int main()  
  22. {  
  23.     MyPrintf("test printf %d %s /n", 1, "hell");  
  24.     Error("test Error %d %s /n", 1, "hell");      
  25.     return 0;  
  26. }  

輸出結果:

test printf 1 hell
err:test Error 1 hell
Press any key to continue

發佈了15 篇原創文章 · 獲贊 15 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章