|
|
laikon wrote:
> Hello, everyone:
>
> this is about overflow in C and C++.
>
> int c = 400;
> printf("%c", c);
>
> it print ? on screen, and ascii of '?' is 63.
No, actually it prints a ? character, which is not defined either in the
basic C character set or in ASCII.
> but
> cout << int(char(400));
>
> it print -112 on screen.
Try this printf statement:
printf("c = %d\nc (cast to unsigned char) = %d\t%c\n",
c, (unsigned char)c, c);
> so, my question is why comes 63 and -112, what relations between them,
> why printf and cout behavior so differently.
Actually they don't behave differently at all. In each case the int
value 400 is being interpreted as an unsigned char. The result of this
conversion gives the value 144, which is not a valid ASCII code.
However all modern systems have one or more "extended" ASCII code pages
active and on your system the value 144 happens to map to the ?
character.
<snip>
|
|