在C,这是为什么的sizeof(char)的1,'A'是一个int? [英] In C, why is sizeof(char) 1, when 'a' is an int?
问题描述
我试过
的printf(%D,%d个\\ N的sizeof(字符)的sizeof('A'));
和获得的 1,4 的作为输出。如果字符的大小是一个,为什么的C的给我4?我想这是因为它是一个整数。所以,当我做字符CH ='C';
有一个隐式转换发生,引擎盖下,从4字节的值设置为1字节值时,它的分配给char变量?
and got 1, 4 as output. If size of a character is one, why does 'c' give me 4? I guess it's because it's an integer. So when I do char ch = 'c';
is there an implicit conversion happening, under the hood, from that 4 byte value to a 1 byte value when it's assigned to the char variable?
推荐答案
在C'一'是一个整型常量(!?!),所以4是你的架构正确。它是隐式转换为char的分配。的sizeof(char)的始终是1的定义。该标准并没有说单位1是什么,但它往往是字节。
In C 'a' is an integer constant (!?!), so 4 is correct for your architecture. It is implicitly converted to char for the assignment. sizeof(char) is always 1 by definition. The standard doesn't say what units 1 is, but it is often bytes.
这篇关于在C,这是为什么的sizeof(char)的1,'A'是一个int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!