为什么的sizeof('A')是在C 4? [英] why sizeof('a') is 4 in C?

查看:177
本文介绍了为什么的sizeof('A')是在C 4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆