简体   繁体   English

printf中“%.6d”是什么意思

[英]What does “%.6d” mean in printf

What does %.6d mean in: %.6d含义是什么:

printf("%s.%.6d len:%d ", timestr, header->ts.tv_usec, header->len);

Is it a typo? 这是一个错字吗?

It seems %.6d is the same as %6d . 似乎%.6d%6d相同。

%.6d

In the .precision format for integer specifiers (d, i, o, u, x, X), precision specifies the minimum number of digits to be written. 在整数说明符(d,i,o,u,x,X)的.precision格式中,precision指定要写入的最小位数。 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. 即使结果较长,也不会截断该值。

%6d

The width (here 6) specifies the minimum number of characters to be printed. 宽度(此处为6)指定要打印的最小字符数。 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. 即使结果较大,也不会截断该值。

Example: 例:

printf("%.6d\n%6d",1,1);

outputs: 输出:

000001
     1

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM