In C, why is sizeof(char) 1, when 'a' is an int?

后端 未结 5 1274
無奈伤痛
無奈伤痛 2020-11-27 06:50

I tried

printf(\"%d, %d\\n\", sizeof(char), sizeof(\'c\'));

and got 1, 4 as output. If size of a character is one, why does \'c\'

5条回答
  •  悲&欢浪女
    2020-11-27 07:10

    It is the normal behavior of the sizeof operator (See Wikipedia):

    • For a datatype, sizeof returns the size of the datatype. For char, you get 1.
    • For an expression, sizeof returns the size of the type of the variable or expression. As a character literal is typed as int, you get 4.

提交回复
热议问题