如何在c中打印符号的整数等价物?

Gor*_*Udo 0 c printf ascii

我正在尝试打印符号的整数等价物;:?. 和空格,但它不起作用。

printf( "Following symbols ending with a whitespace: - = + / \ ; : ? . [whitespace]\n" );

printf( "%d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n", '-', '=', '+', '/', '\', ';', ':', '?', '.', ' ' );
Run Code Online (Sandbox Code Playgroud)

我已经成功打印了 AZ、az、0-20 和其他符号的整数等价物。但是,当我尝试编译此行时,从分号到右侧的符号似乎存在问题。我从 VS 2019 的命令提示符中收到以下错误。

IntegerValueOfChar.c(29): warning C4129: ' ': unrecognized character escape sequence

IntegerValueOfChar.c(31): error C2143: syntax error: missing ')' before 'constant'

IntegerValueOfChar.c(31): warning C4473: 'printf' : not enough arguments passed for format string

IntegerValueOfChar.c(31): note: placeholders and their parameters expect 10 variadic arguments, but 5 were provided

IntegerValueOfChar.c(31): note: the missing variadic argument 6 is required by format string '%d'

IntegerValueOfChar.c(31): error C2137: empty character constant

IntegerValueOfChar.c(31): error C2059: syntax error: ')'
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?我正在使用 notepad++ 和 Visual Studio 编译器。感谢您帮助沮丧的菜鸟。

Ian*_*ott 7

问题是反斜杠字符用作字符常量和字符串文字中的转义前缀。要将实际退格字符表示为字符常量或在字符串文字中,它需要加倍:

printf( "Following symbols ending with a whitspace: - = + / \\ ; : ? . [whispace]\n" );
printf( "%d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n", '-', '=', '+', '/', '\\', ';', ':', '?', '.', ' ' );
Run Code Online (Sandbox Code Playgroud)