Why the sizeof character constant is 4 bytes? [duplicate]

岁酱吖の 提交于 2019-12-08 08:12:57

问题


The program below produces

output - 1 4 4

#include<stdio.h>
void main()
{
    char ch;
    ch='A'; 
    printf("%d %d %d\n",sizeof(ch),sizeof('A'),sizeof(3.2f));

}

Why is the size of character constant is 4 bytes ?


回答1:


Because according to the C standard the type of a character constant is int, and not char.

So in effect, this is the sizeof(int) on your platform.




回答2:


ch is char type so 1 byte.

'A' is int type so 4 bytes. Because in C the character constant is an int type.

Last is float value so 4 bytes.

These values according to the machine you are using.

Edit -

The range of int and float depends on the machine you are using, 16 bit int is as common as 32 bit int.



来源:https://stackoverflow.com/questions/41549293/why-the-sizeof-character-constant-is-4-bytes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!