我是C的新手,在我的一次练习中遇到了一些我无法绕过的东西.当我检查一个tabel元素的大小(这里是'b')比我得到4.然而,如果我检查'char'而不是我得到1.怎么样?
# include <stdio.h>
int main(){
char tabel[10] = {'b','f','r','o','a','u','v','t','o'};
int size_tabel = (sizeof(tabel));
int size_char = (sizeof('b'));
/*edit the above line to sizeof(char) to get 1 instead of 4*/
int length_tabel = size_tabel/size_char;
printf("size_tabel = %i, size_char = %i, lengte_tabel= %i",size_tabel,
size_char,length_tabel);
}
Run Code Online (Sandbox Code Playgroud)
'b'不是char类型.'b'是一个文字,它的类型是int.
从C11标准草案(ISO/IEC 9899:201x):6.4.4.4字符常数:描述
整数字符常量是用单引号括起来的一个或多个多字节字符的序列,如'x'.