为什么 printf 无法正确处理标志、字段宽度和精度? [英] Why printf is not able to handle flags, field width and precisions properly?

查看:67
本文介绍了为什么 printf 无法正确处理标志、字段宽度和精度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发现 printf 的所有功能,并且我已经尝试过:

I'm trying to discover all capabilities of printf and I have tried this :

printf("Test:%+*0d", 10, 20);

打印

测试:%+100d

我首先使用标志 +,然后是宽度 * 并重新使用标志 0.

I have use first the flag +, then the width * and the re-use the flag 0.

为什么要做这个输出?我故意以一种糟糕的方式使用了 printf(),但我想知道为什么它会显示数字 100?

Why it's make this output ? I purposely used printf() in a bad way but I wonder why it shows me the number 100?

推荐答案

这是因为,您向编译器提供了句法废话,因此编译器可以随心所欲.相关阅读,未定义行为.

This is because, you're supplying syntactical nonsense to the compiler, so it is free to do whatever it wants. Related reading, undefined behavior.

在启用警告的情况下编译您的代码,它会告诉您类似的内容

Compile your code with warnings enabled and it will tell you something like

警告:格式[-Wformat=]中的未知转换类型字符0"
printf("测试:%+*0d", 10, 20);
^

warning: unknown conversion type character ‘0’ in format [-Wformat=]
printf("Test:%+*0d", 10, 20);
^

为了正确,该语句应该是

To be correct, the statement should be either of

  • printf("测试:%+*.0d", 10, 20);//注意'.'

其中,0 用作精度

where, the 0 is used as a precision

相关,引用C11,章节§7.21.6.1,(强调我的)

Related, quoting the C11, chapter §7.21.6.1, (emphasis mine)

一个可选精度,它给出了 di 出现的最小位数,ouxX 转换,小数点后出现的位数aAeEf 的字符>F 转换,最大有效次数gG 转换的数字,或为 s 转换写入的最大字节数.精度采用句点 (.) 后跟的形式星号 *(稍后描述)或可选的十进制整数; 如果只有句点是指定时,精度为零.如果精度与任何其他转换说明符,行为未定义.

An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, and X conversions, the number of digits to appear after the decimal-point character for a, A, e, E, f, and F conversions, the maximum number of significant digits for the g and G conversions, or the maximum number of bytes to be written for s conversions. The precision takes the form of a period (.) followed either by an asterisk * (described later) or by an optional decimal integer; if only the period is specified, the precision is taken as zero. If a precision appears with any other conversion specifier, the behavior is undefined.

  • printf("Test:%+0*d", 10, 20);

    其中,0 用作标志.根据语法,所有标志应该一起出现,在任何其他转换规范条目之前,你不能只是把它放在转换规范中的任何地方并期望编译器遵循你的意图.

    where, the 0 is used as a flag. As per the syntax, all the flags should appear together, before any other conversion specification entry, you cannot just put it anywhere in the conversion specification and expect the compiler to follow your intention.

    再次引用,(和我的重点)

    每个转换规范都由字符 % 引入.在%之后,如下依次出现:

    Each conversion specification is introduced by the character %. After the %, the following appear in sequence:

    • 零个或多个标志(以任何顺序)[...]
    • 可选的最小字段宽度 [...]
    • 可选的精度 [...]
    • 可选的长度修饰符 [...]
    • 转换说明符 [....]
    • Zero or more flags (in any order) [...]
    • An optional minimum field width [...]
    • An optional precision [...]
    • An optional length modifier [...]
    • A conversion specifier [....]

  • 这篇关于为什么 printf 无法正确处理标志、字段宽度和精度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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