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

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


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.


Th C standard says that a character literal like 'a' is of type int, not type char. It therefore has (on your platform) sizeof == 4. See this question for a fuller discussion.


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.