C语言 printf中的“%.6d”是什么意思
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2778785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "%.6d" mean in printf
提问by user198729
What does %.6dmean in:
什么%.6d意思:
printf("%s.%.6d len:%d ", timestr, header->ts.tv_usec, header->len);
Is it a typo?
这是一个错字吗?
It seems %.6dis the same as %6d.
好像%.6d是一样的%6d。
回答by codaddict
%.6d
In the .precisionformat for integer specifiers (d, i, o, u, x, X), precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer.
在整数说明符 (d, i, o, u, x, X)的.precision格式中,precision 指定要写入的最小位数。如果要写入的值比这个数字短,则结果用前导零填充。即使结果更长,该值也不会被截断。
%6d
The width (here 6) specifies the minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.
宽度(此处为 6)指定要打印的最小字符数。如果要打印的值比这个数字短,则结果用空格填充。即使结果更大,该值也不会被截断。
Example:
例子:
printf("%.6d\n%6d",1,1);
outputs:
输出:
000001
1
回答by Steve-o
The former will pad with zeros, the latter with spaces.
前者用零填充,后者用空格填充。
#include <stdio.h>
int main(void) {
printf ("%.6d\n", 123);
printf ("%6d\n", 123);
return 0;
}
Produces the following output,
产生以下输出,
000123
123

