why sizeof('a') is 4 in C? [duplicate]

大城市里の小女人 提交于 2019-11-27 08:56:17

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).

The following is the famous line from the famous C book - The C programming Language by Kernighan & Ritchie with respect to a character written between single quotes.

A character written between single quotes represents an integer value equal to the numerical value of the character in the machine's character set.

So sizeof('a') is equivalent to sizeof(int)

'a' by default is an integer and because of that you get size of int in your machine 4 bytes.

char is 1 bytes and because of this you get 1 bytes.

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