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

后端 未结 5 1268
無奈伤痛
無奈伤痛 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:02

    In C 'a' is an integer constant (!?!), so 4 is correct for your architecture. It is implicitly converted to char for the assignment. sizeof(char) is always 1 by definition. The standard doesn't say what units 1 is, but it is often bytes.

提交回复
热议问题