今日のコードにはちょっとした驚きがありました。AIX でコンパイルし、警告レベルを anal に設定して、不正な問題が潜んでいる可能性があるかどうかを確認しました。コードから何か新しいものがクロールされました。
1540-2837 (W) '0' flag is disregarded when combined with
precision and 'i' printf format.
問題のある行を調べた後、それを再現するための小さなプログラムをまとめました。いくつかのプラットフォームでテストしたところ、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;
}
通常、先行ゼロが必要な場合は、「03i」の形式でうまく機能します。
ここで「%.3i」とはどういう意味ですか?
なぜそのような振る舞いをするのですか?