Evi*_*ach 1 c printf text-formatting
今天的代码有些令人惊讶.我在AIX上编译它,将警告级别设置为anal,以查看潜在的问题可能潜伏在哪里.从代码中抓取的新东西.
1540-2837 (W) '0' flag is disregarded when combined with
precision and 'i' printf format.
Run Code Online (Sandbox Code Playgroud)
看完有问题的一行之后,我整理了一个小程序来重现它.在几个平台上测试它表明它不是AIX特有的.
下面的第一个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;
}
Run Code Online (Sandbox Code Playgroud)
通常情况下,如果需要前导零,格式为"03i"可以很好地完成工作.
"%.3i"在这里真正意味着什么?
它为什么会有这样的行为?