James Kanze
10/26/2008 9:58:00 AM
On Oct 25, 11:51 am, questions <mengqidhu...@yahoo.cn> wrote:
> when I type 12456,it will give me "1 2 4 5 6".But when the
> integer is too great ,for example ,if I type 78965,it will not
> give me "7 8 9 6 5",but give me a wrong result,why????
On what machine? I'll suppose a 16 bit machine, since
otherwise, 78965 is not too big.
> #include<stdio.h>
> int main()
> { int x,y,z,m,n,w;
> printf("Give me an integer\n");
> scanf("%d",&x);
If the value read isn't representable in a int, you have
undefined behavior here. The next version of C++ will define
it strictly for operator>>. And from a QoI point of view, I
would expect defined behavior as well, with x being set to
MAX_INT (or MIN_INT, if the value was negative), and an error
being generated. (Of course, since you don't test the return
value of scanf, even if it does detect the error, you wouldn't
know.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34