关联大了
我不看上面的内容,我就无法发现作者这里写错了,有两个错误
1. 首先,怎么定义字符常量'FATE' ?
这样?
char grade = 'FATE';
还是这样?
int grade = 'FATE';
不管是哪样,都是错的
单引号里面只能有一个字符
char grade = '\0';
这里的单引号里面的确是有两个字符,一个 '\' 和一个 '0'
但是这两个字符是一个整体,这两个字符一起用来表示一个转义字符
'\0' 这是一个字符
那么 char grade = 'FATE'; 这是什么?
int grade = 'FATE'; 这又是什么?
单引号里面出现了多个字符,这是错误的
https://www.codenong.com/7755202/
https://www.dii.uchile.cl/~daesp ... 1999_definition.pdf
The value of an integer character constant containing more than one
character (e.g., 'ab'), [...] is implementation-defined.
An integer character constant is a sequence of one or more multibyte characters enclosed
in single-quotes, as in 'x'. A wide character constant is the same, except prefixed by the
letter L. With a few exceptions detailed later, the elements of the sequence are any
members of the source character set; they are mapped in an implementation-defined
manner to members of the execution character set.
An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined. If an integer character constant contains a single character or escape sequence, its value is the one that results when an object with type char whose value is that of the single character or escape sequence is converted to type int.
这属于C语言中的未定义行为,就是不同的编译器很有可能会给出不一样的结果
我就把C语言中的未定义行为理解成是 “错误”
毕竟么,你在这一台电脑上编译的代码可以正常运行
把这个代码复制到另一台电脑,在那台电脑上编译的代码就无法正常运行了
这个版本的编译器编译的代码可以正常运行,升级了一下编译器,重新编译的代码就无法正常运行了
这不算是错误这算是什么?
所以,我就把C语言中的未定义行为理解成是 “错误”
2. char grade = 'B';
'B' 的ascii值是 66,就是0x42
作者的意思是先把0x00000042存储到一个32位的存储单元,然后再取出来,然后再把0x42存储到grade的位置?
但是这个 0x42 是直接包含在机器指令里面的,是直接把0x42存储到grade的位置
不是用一个32位的存储单元中转 |