gpt4 book ai didi

c - 格式化整数时printf中的精度字段

转载 作者:行者123 更新时间:2023-12-04 01:44:49 25 4
gpt4 key购买 nike

当我执行这两行时:

printf("%.5d\n", 3);  // use of precision filed
printf("%05d\n", 3); // use of 0 flag to prepend with 0

我得到以下输出:
00003
00003

同样的结果

所以我想知道整数类型的精度字段是什么意思

最佳答案

对于 %d ,精度是要打印的最小位数。

man page :

The precision

An optional precision, in the form of a period ('.') followed by an optional decimal digit string. Instead of a decimal digit string one may write "*" or "*m$" (for some decimal integer m) to specify that the precision is given in the next argument, or in the m-th argument, respectively, which must be of type int. If the precision is given as just '.', or the precision is negative, the precision is taken to be zero. This gives the minimum number of digits to appear for d, i, o, u, x, and X conversions, the number of digits to appear after the radix character 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 and S conversions.



对于正值,这与为字段宽度和 0 标志提供相同的值相同。如果值为负,负号将占用 %05d 宽度的一个字符。
printf("%.5d\n", -3);  // use of precision filed
printf("%05d\n", -3); // use of 0 flag to prepend with 0

输出:
-00003
-0003

关于c - 格式化整数时printf中的精度字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44394291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com