From: Tanaka Akira Date: 2008-01-27T04:03:47+09:00 Subject: [ruby-dev:33411] printf "%#6.4o\n" printf で %#6.4o により 1, 0, -1 をフォーマットした結果は以 下のようになります。 % ./ruby -ve '1.downto(-1) {|n| printf "%#6.4o\n", n }' ruby 1.9.0 (2008-01-26 revision 15257) [i686-linux] 00001 0000 ..77 C では、(C の %o は符号なしなので 1, 0 について) 以下のよう になります。 % cat tst.c #include int main(int argc, char **argv) { printf("%#6.4o\n", 1); printf("%#6.4o\n", 0); return 0; } % gcc -Wall tst.c % ./a.out 0001 0000 width が 6 なので 6桁になる C の結果が適切だと思います。 それとの一貫性からして、-1 のときの " ..77" も 6桁になるよう 空白がもうひとつ必要だと思います。 また、1 をフォーマットした結果が " 0001" になるのは、8進の代 替形式は precision を増やすような動作で、1 の場合 4桁ですで に先頭が 0 なので precision を増やさないためだと思います。規 格 (ANSI C) には以下のように書いてあります。 # The result is converted to an ``alternative form''. For o conversion, it increases the precision, if and only if necessary, to force the first digit of the result to be a zero (if the value and precision are both 0, a single 0 is printed). -- [田中 哲][たなか あきら][Tanaka Akira]