代码之家  ›  专栏  ›  技术社区  ›  EvilTeach

为什么“%.3i”打印前导零?

  •  1
  • EvilTeach  · 技术社区  · 17 年前

     1540-2837 (W) '0' flag is disregarded when combined with 
                   precision and 'i' printf format.
    

    下面的第一个printf模拟了程序中的结果。

    #include <stdio.h>    
    int main(void)
    {
        int x = 3;
    
        printf("Format 0.3i <%0.3i>\n", x);  // prints 003, and AIX does a warning
        printf("Format  .3i <%.3i>\n", x);   // prints 003, with no warning
        printf("Format   3i <%3i>\n", x);    // prints   3, with no warning.
    
        return 0;
    }
    

    2 回复  |  直到 13 年前
        1
  •  5
  •   Daniel LeCheminant    17 年前

    从a doc on printf

    对于整数说明符(d,i,o,u,x,x):precision指定要写入的最小位数。如果要写入的值小于此数,则结果将用前导零填充。即使结果较长,该值也不会被截断。精度为0表示没有为值0写入字符

        2
  •  0
  •   Sean Bright Sean Stinehour    17 年前

    自 man 3 printf

    数值转换(d、i、o、u、x和 X) ,0标志被忽略。

    这 . 正在指定精度,因此 0 被忽略。至于它的“为什么”,你必须问C标准的作者:)