填充的printf由0弦乐 [英] Padding printf for Strings by 0

查看:76
本文介绍了填充的printf由0弦乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法来代替空格字符0 printf中的填充字段宽度

Is there a way to replace the space character to 0 in printf padding for field width

使用code

printf("%010s","this");

似乎没有字符串工作!

Doesnt seem to work for strings!!

推荐答案

事实上, 0 标志仅适用于数字转换。你将不得不手工做:

Indeed, the 0 flag only works for numeric conversions. You will have to do this by hand:

int print_padleftzeroes(const char *s, size_t width)
{
    size_t n = strlen(s);
    if(width < n)
        return -1;
    while(width > n)
    {
        putchar('0');
        width--;
    }
    fputs(s, stdout);
    return 0;
}

这篇关于填充的printf由0弦乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆