为什么 %c 格式说明符在以下代码中不起作用?

min*_*als -3 c printf clang

我刚刚开始阅读 C 并且目前正在使用格式说明符。下面是代码示例:

#include <stdio.h>


int main(void) {
    char code = 'a' - 'A';
    printf("\n>>>%c (%d)", code, code);
    printf("\n>>>%c", 32);

    char incode;
    printf("\n\nGive me some char: ");
    scanf("%c", &incode);
    printf("\n>>>%c (%d)", incode, incode);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

PS C:\ex> ./print

>>>  (32)
>>>

Give me some char: A

>>>A (65)
Run Code Online (Sandbox Code Playgroud)

那么,为什么%c在最后有效printf而在开始时无效?我在 Windows 和 Linux 上测试了这个示例,两者的行为都相同。

  • Windows 编译器: gcc.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
  • Linux编译器: clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)

All*_*ind 6

ASCII 32 是' '(空格)。印刷好了,就是看不到。
如果您将格式字符串更改为 ,"\n>>>%c<< (%d)"您将在(双关语)负空间中看到它。
您还可以通过输出od -a进行确认。