加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C printf 0标志和宽度属性与精度标志之间的差异

发布时间:2020-12-16 10:04:35 所属栏目:百科 来源:网络整理
导读:我目前正在学习libc的printf函数,我不明白,有什么区别: printf("Test : %010d",10); 使用0标志和10作为宽度说明符 和 printf("Test : %.10d",10); 使用10作为精度说明符 产生相同的输出:测试:0000000010 解决方法 我们将从 the docs for printf()开始,我
我目前正在学习libc的printf函数,我不明白,有什么区别:

printf("Test : %010d",10);

使用0标志和10作为宽度说明符

printf("Test : %.10d",10);

使用10作为精度说明符

产生相同的输出:测试:0000000010

解决方法

我们将从 the docs for printf()开始,我将突出他们的相关位.

前0填充.

`0′ (zero)

Zero padding. For all conversions except n,the converted value is padded on the left with zeros rather than blanks. If a precision is given with a numeric conversion (d,i,o,u,x,and X),the 0 flag is ignored.

再精确.

An optional precision,in the form of a period . followed by an optional digit string. If the digit string is omitted,the precision is taken as zero. This gives the minimum number of digits to appear for d,and X conversions,the number of digits to appear after the decimal-point for a,A,e,E,f,and F conversions,the maximum number of significant digits for g and G conversions,or the maximum number of characters to be printed from a string for s conversions.

?0d表示零填充到最小宽度为10位.没问题.

%.10d“,因为你正在使用%d,所以要显示的最小位数是10.所以与零填充相同.%.10f会表现得更像你期望的那样.

我建议你使用?0d来零垫. %.10d表单是一个令人惊讶的功能,可能会让读者感到困惑.我不知道它,我很惊讶它不会被简单地忽略.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读