comp.lang.c
[Top] [All Lists]

Re: printf and cout

Subject: Re: printf and cout
From: santosh
Date: Wed, 26 Mar 2008 15:55:26 +0530
Newsgroups: comp.lang.c

santosh wrote:

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

Actually it is used as a placeholder by your system to indicate that an
appropriate conversion could not be done.


<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy