glibc 的 printf("% .0d",0) 打印空格,printf("%+.0d",0)
打印加号,glib printf测试检查此行为。但这是正确的吗?根据标准(7.19.6.1):
+ 有符号转换的结果始终以加号或减号开头。 (如果未指定此标志,则仅当转换负值时才以符号开头。)
space 如果带符号转换的第一个字符不是符号,或者带符号转换结果不包含任何字符,则会在结果前面添加一个空格。如果空格和 + 标志同时出现,则忽略空格标志。
但是,关于 d
说明符的精度:
以零精度转换零值的结果是无字符。
当然,这个“无字符”规则并不排除将字段填充到请求的宽度,但据我所知,前缀空格或+
是转换的一部分,因此遵守“无字符”规则。
这个问题有没有澄清过?
glibc's printf("% .0d",0)
prints a space, and printf("%+.0d",0)
prints a plus sign, and the glib printf test checks for this behavior. But is it correct? From the standard (7.19.6.1):
+ The result of a signed conversion always begins with a plus or minus sign. (It begins with a sign only when a negative value is converted if this flag is not specified.)
space If the first character of a signed conversion is not a sign, or if a signed conversion results in no characters, a space is prefixed to the result. If the space and + flags both appear, the space flag is ignored.
However, regarding precision for the d
specifier:
The result of converting a zero value with a precision of zero is no characters.
Naturally this "no characters" rule does not preclude padding the field to the requested width, but as far as I can tell, the prefixed space or +
is part of the conversion, and thus subject to the "no characters" rule.
Has this matter ever been clarified?
发布评论
评论(1)
不,它是在谈论没有修饰符的基本转换;这就是为什么修饰符特别说明当基本转换不产生字符时它们会做什么。
No, it's talking about the basic conversion without modifiers; that's why the modifiers specifically say what they do when the basic conversion produces no characters.