为什么的sizeof('A')是在C 4? [英] why sizeof('a') is 4 in C?
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/433895/why-are-c-character-literals-ints-instead-of-chars\">Why是C字符文字整数,而不是字符?
块引用>#包括LT&;&stdio.h中GT;
INT主要(无效)
{
焦炭B ='C';
的printf(这里的大小是祖%\\ N的sizeof('A'));
的printf(这里的大小是%俎,的sizeof(B));
}在这里输出(见现场演示 rel=\"nofollow\">。)
这里大小为4
这里大小为1我没有得到为什么
的sizeof('A')
4解决方案因为在C字符常量,比如'A'具有类型
INT
。有一个Ç常见问题解答这个主体探析:
令人惊讶的是,用C字符常量是int型,所以
的sizeof('A')中的sizeof(int)的(虽然这是另一个领域,C ++
不同)。
块引用>Possible Duplicate:
Why are C character literals ints instead of chars?
#include<stdio.h> int main(void) { char b = 'c'; printf("here size is %zu\n",sizeof('a')); printf("here size is %zu",sizeof(b)); }
here output is (See live demo here.)
here size is 4 here size is 1
I am not getting why
sizeof('a')
is 4 ?解决方案Because in C character constants, such as 'a' have the type
int
.There's a C FAQ about this suject:
Perhaps surprisingly, character constants in C are of type int, so sizeof('a') is sizeof(int) (though this is another area where C++ differs).
这篇关于为什么的sizeof('A')是在C 4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!