可能重复: 为什么C字符文字是ints而不是字符?
#include<stdio.h>
int main(void)
{
char b = 'c';
printf("here size is %zu\n",sizeof('a'));
printf("here size is %zu",sizeof(b));
}这里的输出是(参见live 这里)。
here size is 4
here size is 1我不明白为什么sizeof('a')是4?
发布于 2011-12-28 10:45:46
因为在C字符常量中,例如'a‘具有int类型。
关于这个对象有一个C常见问题:
也许令人惊讶的是,C中的字符常量是int类型,所以sizeof of (‘a’)是sizeof of(Int)(尽管这是C++不同的另一个方面)。
发布于 2011-12-28 11:08:29
下面是著名的C书“The C programming Language by Kernighan & Ritchie”中关于单引号之间的字符的著名行。
A character written between single quotes represents an integer value equal to the numerical value of the character in the machine's character set.
所以sizeof('a')等同于sizeof(int)
发布于 2011-12-28 10:47:08
在默认情况下,‘’是一个整数,因此在机器中可以得到4个字节的int大小。
char是1字节,正因为如此,你得到了1字节。
https://stackoverflow.com/questions/8654837
复制相似问题