这里发生了什么?
#include <stdio.h>
int main (void)
{
int x = 'HELL';
printf("%d\n", x);
return 0;
}
打印 1212501068
我预计会出现编译错误。
欢迎解释=)
最佳答案
1212501068
十六进制为 0x48454c4c
。
0x48
是H
的 ASCII 代码。0x45
是E
的 ASCII 代码。0x4c
是L
的 ASCII 代码。0x4c
是L
的 ASCII 代码。
请注意,此行为是实现定义的,因此不可移植。一个好的编译器会发出警告:
$ gcc test.c
test.c: In function 'main':
test.c:4:11: warning: multi-character character constant [-Wmultichar]
关于c - 奇怪的 C 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18677083/