Take the 2-minute tour ×

Check out this program


int main (){

char c='a';
printf("%d %d", sizeof(c),sizeof('a'));

the output is 1 4
I know when we write a statement char c='a';

then how does it happen that in space of 1 byte (char c) some thing of 4 bytes (ASCII code) is stored why is there no overflow etc.

share|improve this question
The compiler is smart. :) –  muntoo Jun 19 '11 at 5:34
Curiously, sizeof('a') is one of the differences between C and C++ (so the latter isn't a strict superset of the former): In C, sizeof('a') == sizeof(int), in C++, sizeof('a') == sizeof(char) == 1. –  Kerrek SB Jun 19 '11 at 11:07
add comment

3 Answers

up vote 15 down vote accepted

First, per ANSI/IEC 9899:1999(E) §

 10. An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. [...]


 2. The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. [...]

 3. When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1. [...]

As you can see, since the type of a character constant is int, for sizeof('a') we get sizeof(int), which is 4 on your platform. However, for sizeof(c), we get the size of a char, which is defined to be 1.

So why can we assign 'a' to a char?



So, the int that is 'a' is implicitly converted to a char. There's an example in there as well, showing explicitly that ints can be implicitly converted to char.

share|improve this answer
And if you want to be really pedantic, cite also, "if the value can be represented in the new type, it is unchanged", and 6.2.5/3, "char is large enough to store any member of the basic execution character set", and 5.2.1/3 "the basic execution character set shall have ... the 26 lowercase letters of the Latin alphabet" :-) –  Steve Jessop Jun 19 '11 at 11:19
add comment

The compiler implicitly converts the int to char.

int i = 42;
char c = i * 2 - 4;

That last line is interpreted by the compiler as:

char c = (char)(i * 2 - 4);

These implicit type conversions are handled by the compiler - there's no "buffer overflow". The (char) is handled internally (by the machine itself, most likely, for simple types like int). It appropriately cuts down on the extra bytes and preserves the "signedness" (+/-).

share|improve this answer
add comment

"A character literal has type int" (http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7l.doc%2Flanguage%2Fref%2Fclrc02ccon.htm)

But C lets you make theoretically "unsafe" automatic casts -- it's ok to for example,

char c = 34;

even though 34 is clearly a 4-byte int. What makes this safe is that you know when you write 'a' that it's really 1 ascii character and hence 1 byte.

Nice question by the way -- confused me for a bit.

share|improve this answer
add comment

Your Answer


