Help! help!

Q

questions

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

#include<stdio.h>
int main()
{ int x,y,z,m,n,w;

printf("Give me an integer\n");
scanf("%d",&x);

y=x/10000;
z=x%10000/1000;
m=x%1000/100;
n=x%100/10;
w=x%10;

printf("%d %d %d %d %d",y,z,m,n,w);

return 0;}
 
K

Kai-Uwe Bux

questions said:
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????

#include<stdio.h>
int main()
{ int x,y,z,m,n,w;

printf("Give me an integer\n");
scanf("%d",&x);

y=x/10000;
z=x%10000/1000;
m=x%1000/100;
n=x%100/10;
w=x%10;

printf("%d %d %d %d %d",y,z,m,n,w);

return 0;}

Just a data point: when I try your code with g++ version 4.3.1 and 78965 as
input, I get exactly

7 8 9 6 5

as the output.


Best

Kai-Uwe Bux
 
R

Rolf Magnus

questions said:
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????

Well, depending on the target platform, the range of int can be as low
as -32767 ... +32767.
 
A

Andre Kostur

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

#include<stdio.h>
int main()
{ int x,y,z,m,n,w;

printf("Give me an integer\n");
scanf("%d",&x);

y=x/10000;
z=x%10000/1000;
m=x%1000/100;
n=x%100/10;
w=x%10;

printf("%d %d %d %d %d",y,z,m,n,w);

return 0;}

Because an int can only hold a certain size of number. The acutal size is
dependant on your platform. You do know that it can hold at least as large
as a short, and no more than a long. (Offhand it seems that your int is a
16-bit value, so the max number here is 32767. int's are signed so the
full range would be -32768 - 32767).
 
L

Lightning

Well, depending on the target platform, the range of int can be as low
as -32767 ... +32767.

To elaborate on the post above, just make x,y,z,m,n,w long.
 
J

James Kanze

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.)
 
P

peter koch

(e-mail address removed):











Because an int can only hold a certain size of number.  The acutal size is
dependant on your platform.  You do know that it can hold at least as large
as a short, and no more than a long.  (Offhand it seems that your int is a
16-bit value, so the max number here is 32767.  int's are signed so the
full range would be -32768 - 32767).

I agree with your diagnosis. but it is still a little weird: I would
suspect that most developers today would either be working on a
platform where an integer is 32 bits or be sufficiently knowledgeable
that they would know the answer to the problem immediately.

/Peter
 
O

osmium

:

<quote>
(e-mail address removed):









Because an int can only hold a certain size of number. The acutal size is
dependant on your platform. You do know that it can hold at least as large
as a short, and no more than a long. (Offhand it seems that your int is a
16-bit value, so the max number here is 32767. int's are signed so the
full range would be -32768 - 32767).

I agree with your diagnosis. but it is still a little weird: I would
suspect that most developers today would either be working on a
platform where an integer is 32 bits or be sufficiently knowledgeable
that they would know the answer to the problem immediately.
<end quote>

My guess would be that the person who asked the question was not, by any
stretch of the imagination, a developer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top