Re: printf and cout
On Mar 26, 11:50 pm, Paul Brettschneider
<paul.brettschnei...@yahoo.fr> wrote:
laikon wrote:
this is about overflow in C and C++.
int c = 400;
printf("%c", c);
it print ? on screen, and ascii of '?' is 63.
but
cout << int(char(400));
it print -112 on screen.
These two things are not the same at all.
The printf version should read like this:
#include <cstdio>
int main()
{
int c = 400;
printf("%d", (char)400);
}
And, not surprisingly, it prints -112 too (on a 2s complement machine).
On a 2's complement machine with 8 bit bytes, where plain char
is signed. Exceptions to the first two conditions are fairly
rare today. But unsigned plain char is an option with a lot of
compilers, and the default on some as well (and would make life
a lot easier if you could count on it).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34