在 printf 中打印八进制文字

Printing octal literal in printf

打印八进制文字的正确方法是什么?例如,以下适用于十六进制数字 \x 但不适用于八进制 [=12=]

printf("\x66 02\n");

f 2

如何做到这一点?

八进制字面量由 1 到 3 个数字组成。不支持像 02 这样的 4 位数序列。这似乎被视为两个字符:02.

你想要的可能是printf("\x66 2\n");。如果使用 ASCII,这将打印 f B

引自N1570 6.4.4.4 字符常量:

octal-escape-sequence:
\ octal-digit
\ octal-digit octal-digit
\ octal-digit octal-digit octal-digit

The octal digits that follow the backslash in an octal escape sequence are taken to be part of the construction of a single character for an integer character constant or of a single wide character for a wide character constant. The numerical value of the octal integer so formed specifies the value of the desired character or wide character.