实现自己的printf函数

转载:https://blog.csdn.net/zy799894671/article/details/7761038

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);//这里输出到dos窗口

//使用串口输出一个字符的函数替换调这个,即可实现输出到嵌入式串口

//USART_SendData(USART1, 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;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值