打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
实现自己的printf函数

http://blog.csdn.net/zy799894671/article/details/7761038

2012

my_printf.h:

[csharp] view plain copy
  1. extern void my_printf(const char *format,...);  


 

my_printf.c:

[csharp] view plain copy
  1. #include "my_printf.h"  
  2. #include "stdarg.h"  
  3. /********************************************************** 
  4.  
  5. ***********************************************************/  
  6. void printch(const char ch)  
  7. {  
  8.     putchar(ch);  
  9. }  
  10. /********************************************************** 
  11.  
  12. ***********************************************************/  
  13. void printint(const int dec)  
  14. {  
  15.     if(dec == 0)  
  16.     {  
  17.         return;  
  18.     }  
  19.     printint(dec / 10);  
  20.     putchar((char)(dec % 10 + '0'));  
  21. }  
  22. /********************************************************** 
  23.  
  24. ***********************************************************/  
  25. void printstr(const char *ptr)  
  26. {  
  27.     while(*ptr)  
  28.     {  
  29.         putchar(*ptr);  
  30.     ptr++;  
  31.     }  
  32. }  
  33. /********************************************************** 
  34.  
  35. ***********************************************************/  
  36. void printfloat(const float flt)  
  37. {  
  38.     int tmpint = (int)flt;  
  39.     int tmpflt = (int)(100000 * (flt - tmpint));  
  40.     if(tmpflt % 10 >= 5)  
  41.     {  
  42.         tmpflt = tmpflt / 10 + 1;  
  43.     }  
  44.     else  
  45.     {  
  46.         tmpflt = tmpflt / 10;  
  47.     }  
  48.     printint(tmpint);  
  49.     putchar('.');  
  50.     printint(tmpflt);  
  51.   
  52. }  
  53. /********************************************************** 
  54.  
  55. ***********************************************************/  
  56. void my_printf(const char *format,...)  
  57. {  
  58.     va_list ap;  
  59.     va_start(ap,format);  
  60.     while(*format)  
  61.     {  
  62.         if(*format != '%')  
  63.     {  
  64.             putchar(*format);  
  65.         format++;  
  66.     }  
  67.     else  
  68.     {  
  69.         format++;  
  70.         switch(*format)  
  71.         {  
  72.             case 'c':  
  73.             {  
  74.                 char valch = va_arg(ap,int);  
  75.             printch(valch);  
  76.             format++;  
  77.             break;  
  78.             }  
  79.             case 'd':  
  80.             {  
  81.                 int valint = va_arg(ap,int);  
  82.             printint(valint);  
  83.             format++;  
  84.             break;  
  85.             }  
  86.         case 's':  
  87.         {  
  88.             char *valstr = va_arg(ap,char *);  
  89.             printstr(valstr);  
  90.             format++;  
  91.             break;  
  92.         }  
  93.         case 'f':  
  94.         {  
  95.             float valflt = va_arg(ap,double);  
  96.             printfloat(valflt);  
  97.             format++;  
  98.             break;  
  99.         }  
  100.         default:  
  101.         {  
  102.             printch(*format);  
  103.             format++;  
  104.         }  
  105.         }  
  106.         va_end(ap);  
  107.     }  
  108.     }  
  109. }  
  110. /********************************************************** 
  111.  
  112. ***********************************************************/  
  113. int main()  
  114. {  
  115.     char ch = 'A';  
  116.     char *str = "holle world";  
  117.     int dec = 1234;  
  118.     float flt = 1234.3456789;   
  119.     my_printf("str = %s,dec = %d,ch = %c,flt = %f\n",str,dec,ch,flt);  
  120.     return 0;  
  121. }  

编译一下,运行一下,ok了。

(注:float型我这能输出小数点后4位,第5位四舍五入了)

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
bash Linux 文件判断,if
c51 函数库函数 STDIO.H:一般I/O函数
短小精悍,最全介绍,C语言输入输出printf、scanf
扒一扒中断为什么不能调printf
C的可变参数
C语言 sscanf用法详解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服