实现自己的printf函数
来源:互联网 发布:js获取汉字拼音首字母 编辑:程序博客网 时间:2024/05/06 19:16
my_printf.h:
extern void my_printf(const char *format,...);
my_printf.c:
#include "my_printf.h"#include "stdarg.h"/*********************************************************************************************************************/void printch(const char ch){ putchar(ch);}/*********************************************************************************************************************/void printint(const int dec){ if(dec == 0) { return; } printint(dec / 10); putchar((char)(dec % 10 + '0'));}/*********************************************************************************************************************/void printstr(const char *ptr){ while(*ptr) { putchar(*ptr);ptr++; }}/*********************************************************************************************************************/void printfloat(const float flt){ int tmpint = (int)flt; int tmpflt = (int)(100000 * (flt - tmpint)); if(tmpflt % 10 >= 5) { tmpflt = tmpflt / 10 + 1; } else { tmpflt = tmpflt / 10; } printint(tmpint); putchar('.'); printint(tmpflt);}/*********************************************************************************************************************/void my_printf(const char *format,...){ va_list ap; va_start(ap,format); while(*format) { if(*format != '%'){ putchar(*format); format++;}else{ format++; switch(*format) { case 'c': { char valch = va_arg(ap,int); printch(valch); format++; break; } case 'd': { int valint = va_arg(ap,int); printint(valint); format++; break; }case 's':{ char *valstr = va_arg(ap,char *); printstr(valstr); format++; break;}case 'f':{ float valflt = va_arg(ap,double); printfloat(valflt); format++; break;}default:{ printch(*format); format++;} } va_end(ap);} }}/*********************************************************************************************************************/int main(){ char ch = 'A'; char *str = "holle world"; int dec = 1234; float flt = 1234.3456789; my_printf("str = %s,dec = %d,ch = %c,flt = %f\n",str,dec,ch,flt); return 0;}
编译一下,运行一下,ok了。
(注:float型我这能输出小数点后4位,第5位四舍五入了)
- 实现自己的printf函数
- 实现自己的printf函数
- 实现自己的printf函数
- 实现自己的printf函数
- 自己实现printf函数
- 自己实现printf函数
- 实现自己的嵌入式printf函数
- 实现自己的变参函数printf
- 实现自己的printf函数(转载)
- 自己实现的简单Printf函数
- 实现自己的printf函数(1)
- 实现自己的printf函数(2)
- 自己实现printf函数,用...
- 自己实现一个printf函数
- 自己实现printf()函数
- 实现自己的不定参数函数(类似printf)
- 嵌入式 实现自己的printf函数小示例
- 自己写的printf函数
- EPON配置规范
- 了凡四训——改变命运之学
- oracle 物化视图
- Mapkit之在地圖上加入座標點,使用MKAnnotation和MKAnnotationView
- poj2031
- 实现自己的printf函数
- PeopleTools Tip: PeopleCode Dump
- 最大流
- c#遍历一个文件夹下的所有文件包括子文件夹
- 回顾MySpace架构的坎坷之路
- 在64位Win7下为HP LaserJet 1012安装网络打印机驱动
- C++ 的 get_time 和 put_time 函数
- 摩托罗拉ME525双WIPE步骤
- csdn_交叉编译工具命令