开发者

int is 4 bytes but still it can be stored in char why is there no overflow

Check out thi开发者_如何学编程s program

#include<stdio.h>

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.


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

 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. [...]

§6.5.3.4:

 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?

§6.5.16.1:

 2. In simple assignment (=), the value of the right operand is converted to the type of the assignment expression and replaces the value stored in the object designated by the left operand.

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.


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" (+/-).


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜