我刚刚开始阅读 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 上测试了这个示例,两者的行为都相同。
gcc.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)ASCII 32 是' '(空格)。印刷好了,就是看不到。
如果您将格式字符串更改为 ,"\n>>>%c<< (%d)"您将在(双关语)负空间中看到它。
您还可以通过输出od -a进行确认。
| 归档时间: |
|
| 查看次数: |
77 次 |
| 最近记录: |