c中printf语句中的%.#s格式说明符 [英] %.#s format specifier in printf statement in c
问题描述
请解释输出.printf()
中的%.#s
是什么意思?
Please explain the output. What does %.#s
in printf()
mean?
#include<stdio.h>
#include <stdlib.h>
int main(int argc,char*argv[]){
char *A="HELLO";
printf("%.#s %.2s\n",A,A);
return 0;
}
输出:
#s HE
推荐答案
这是未定义的行为.#
在printf
格式说明符中表示替代形式,但根据标准,#
仅与o
一起使用、a
、A
、x
、X
、e
、E
、f
、F
、g
、G
,不包括s
>.
It's undefined behavior. #
in printf
format specifier means alternative form, but according to the standard, #
is only used together with o
, a
, A
, x
, X
, e
, E
, f
, F
, g
, G
, not including s
.
#
结果被转换为替代形式".对于 o
转换,它增加精度,当且仅在必要时,强制结果的第一位数字为零(如果值和精度都是 0
,则打印单个 0
).对于 x
(或 X
)转换,非零结果带有 0x
(或 0X
)前缀.对于a
、A
、e
、E
、f
、F
、g
和 G
转换,总是转换浮点数的结果包含小数点字符,即使后面没有数字.(通常,这些转换的结果中才会出现小数点字符,前提是它后面跟着一个数字.)对于 g
和 G
转换,尾随零不会从结果.对于其他转换,行为未定义.
C11 §7.21.6.1 The
fprintf
function Section 6
#
The result is converted to an ‘‘alternative form’’. Foro
conversion, it increases the precision, if and only if necessary, to force the first digit of the result to be a zero (if the value and precision are both0
, a single0
is printed). Forx
(orX
) conversion, a nonzero result has0x
(or0X
) prefixed to it. Fora
,A
,e
,E
,f
,F
,g
, andG
conversions, the result of converting a floating-point number always contains a decimal-point character, even if no digits follow it. (Normally, a decimal-point character appears in the result of these conversions only if a digit follows it.) Forg
andG
conversions, trailing zeros are not removed from the result. For other conversions, the behavior is undefined.
例如,在我的机器上,输出是不同的:%.0#s HE
For example, on my machine, output is different: %.0#s HE
这篇关于c中printf语句中的%.#s格式说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!