printf("%.d", 0) 和 printf("%.1d", 0) 有什么区别?
What's the difference between printf("%.d", 0) and printf("%.1d", 0)?
我正在重新编码 printf
,现在我被精确标志封锁了一会儿。所以我读到类型转换说明符为 d
时的默认精度为 1:
所以我认为%.d
和%.1d
之间没有区别,但是当我测试时:
printf(".d =%.d, .1d= %.1d", 0, 0);
我确实找到了一个:
.d =, .1d= 0
如果在%
之后使用.
而不指定精度,则设置为零。
From the printf
page on cppreference.com:
.
followed by integer number or *, or neither that specifies
precision of the conversion. In the case when * is used, the precision
is specified by an additional argument of type int. If the value of
this argument is negative, it is ignored. If neither a number nor *
is used, the precision is taken as zero.
如果使用%d
(不使用.
)则默认为1
:
printf("d = %d, 1d= %1d", 0, 0);
# Output: d = 0, 1d= 0
C18 标准 - ISO/IEC 9899:2018 -(强调我的)状态:
"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 non negative 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."
Source: C18, §7.21.6.1/4
表示 %.d
等于 %.0d
而与 %.1d
不同。
此外:
"d,i - The int argument is converted to signed decimal in the style [-]dddd. The precision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is no characters."
Source: C18, §7.21.6.1/8
这意味着如果您在 printf()
调用中使用 %.d
转换 0
值,则结果保证不会打印任何字符(这符合您的测试体验) .
当精度设置为零或它的值被省略时
printf( "%.d", x )'
当根据转换说明符d和i的描述时(7.21.6.1 fprintf函数)
The int argument is converted to signed decimal in the style [−]dddd.
The precision specifies the minimum number of digits to appear; if the
value being converted can be represented in fewer digits, it is
expanded with leading zeros. The default precision is 1. The result
of converting a zero value with a precision of zero is no
characters.
这是一个演示程序
#include <stdio.h>
int main(void)
{
printf( "%.d\n", 0 );
printf( "%.0d\n", 0 );
printf( "%.1d\n", 0 );
return 0;
}
它的输出是
0
也就是当精度等于0或者它的值不存在时,如果指定0作为参数,则什么都不会输出。
我正在重新编码 printf
,现在我被精确标志封锁了一会儿。所以我读到类型转换说明符为 d
时的默认精度为 1:
所以我认为%.d
和%.1d
之间没有区别,但是当我测试时:
printf(".d =%.d, .1d= %.1d", 0, 0);
我确实找到了一个:
.d =, .1d= 0
如果在%
之后使用.
而不指定精度,则设置为零。
From the printf
page on cppreference.com:
.
followed by integer number or *, or neither that specifies precision of the conversion. In the case when * is used, the precision is specified by an additional argument of type int. If the value of this argument is negative, it is ignored. If neither a number nor * is used, the precision is taken as zero.
如果使用%d
(不使用.
)则默认为1
:
printf("d = %d, 1d= %1d", 0, 0);
# Output: d = 0, 1d= 0
C18 标准 - ISO/IEC 9899:2018 -(强调我的)状态:
"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 non negative 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."
Source: C18, §7.21.6.1/4
表示 %.d
等于 %.0d
而与 %.1d
不同。
此外:
"d,i - The int argument is converted to signed decimal in the style [-]dddd. The precision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is no characters."
Source: C18, §7.21.6.1/8
这意味着如果您在 printf()
调用中使用 %.d
转换 0
值,则结果保证不会打印任何字符(这符合您的测试体验) .
当精度设置为零或它的值被省略时
printf( "%.d", x )'
当根据转换说明符d和i的描述时(7.21.6.1 fprintf函数)
The int argument is converted to signed decimal in the style [−]dddd. The precision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is no characters.
这是一个演示程序
#include <stdio.h>
int main(void)
{
printf( "%.d\n", 0 );
printf( "%.0d\n", 0 );
printf( "%.1d\n", 0 );
return 0;
}
它的输出是
0
也就是当精度等于0或者它的值不存在时,如果指定0作为参数,则什么都不会输出。