473,395 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Help! help!

Tell me what fault it is ?

when I type 12345,it'll give me "1 2 3 4 5",but when the integer I
type in is too great ,it'll not give me what I want.For example,if I
type 78569,I want to get "7 8 5 6 9",but the result is
wrong.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;}
Oct 25 '08 #1
14 2026

"questions" <me**********@yahoo.cnwrote in message
news:4a**********************************@z6g2000p re.googlegroups.com...
Tell me what fault it is ?

when I type 12345,it'll give me "1 2 3 4 5",but when the integer I
type in is too great ,it'll not give me what I want.For example,if I
type 78569,I want to get "7 8 5 6 9",but the result is
wrong.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;}
Convert 12345 to hex.
How many bytes do you need to store that number?
Convert 78569 to hex.
How many bytes do you need to store that number?

How many bytes in an int?

Richard
Oct 25 '08 #2
questions <me**********@yahoo.cnwrites:
Tell me what fault it is ?

when I type 12345,it'll give me "1 2 3 4 5",but when the integer I
type in is too great ,it'll not give me what I want.For example,if I
type 78569,I want to get "7 8 5 6 9",but the result is
wrong.why?????
Not for me. I get 7 8 5 6 9. Presumably your system has INT_MAX less
than mine.
#include<stdio.h>
int main()
{ int x,y,z,m,n,w;
Try "long int" instead.
printf("Give me an integer\n");
scanf("%d",&x);
and "%ld" to match here
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);
and here.
return 0;}
--
Ben.
Oct 25 '08 #3
questions wrote:
when I type 12345,it'll give me "1 2 3 4 5",but when the integer I
type in is too great ,it'll not give me what I want.For example,if I
type 78569,I want to get "7 8 5 6 9",but the result is
wrong.why?????
#include<stdio.h>
int main()
{ int x,y,z,m,n,w;

printf("Give me an integer\n");
scanf("%d",&x);
x is too large to fit into an int. if you want numbers greater than the
size of an int on your PC, use a larger type such as long.

Also: don't use scanf - its an unsafe function for beginners due to the
ease with which you can create buffer overruns.

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Oct 25 '08 #4

"questions" <me**********@yahoo.cnwrote in message
news:4a**********************************@z6g2000p re.googlegroups.com...
Tell me what fault it is ?

when I type 12345,it'll give me "1 2 3 4 5",but when the integer I
type in is too great ,it'll not give me what I want.For example,if I
type 78569,I want to get "7 8 5 6 9",but the result is
wrong.why?????
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);
Your question has been answered. However, you want to find out how to use
loops more. If you wanted more digits, you'd need a lot more typing with
your method. Just one example of many possibilties:

#include<stdio.h>

/* extract n'th digit of x (right-most is #1) */
int gd(int x, int n)
{ int d=1;

if (x<0) x=-x;
while (--n) d*=10;
return x%(d*10)/d; /* Using your method */
}

int main(void)
{ int i,x;
# define ndigits 5

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

for (i=ndigits; i>=1; --i) printf("%d ",gd(x,i));
puts("");

return 0;
}

--
bartc

Oct 25 '08 #5
On 10月25日, 下午6时59分, "Richard Seriani" <richard_s...@cox.netwrote:
"questions" <mengqidhu...@yahoo.cnwrote in message

news:4a**********************************@z6g2000p re.googlegroups.com...


Tell me what fault it is ?
when I type 12345,it'll give me "1 2 3 4 5",but when the integer I
type in is too great ,it'll not give me what I want.For example,if I
type 78569,I want to get "7 8 5 6 9",but the result is
wrong.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;}

Convert 12345 to hex.
How many bytes do you need to store that number?
Convert 78569 to hex.
How many bytes do you need to store that number?

How many bytes in an int?

Richard- 隐藏被引用文字 -

- 显示引用的文字 -
5 bytes
Oct 26 '08 #6
On 10月25日, 下午7时20分, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
questions <mengqidhu...@yahoo.cnwrites:
Tell me what fault it is ?
when I type 12345,it'll give me "1 2 3 4 5",but when the integer I
type in is too great ,it'll not give me what I want.For example,if I
type 78569,I want to get "7 8 5 6 9",but the result is
wrong.why?????

Not for me. I get 7 8 5 6 9. Presumably your system has INT_MAX less
than mine.
#include<stdio.h>
int main()
{ int x,y,z,m,n,w;

Try "long int" instead.
printf("Give me an integer\n");
scanf("%d",&x);

and "%ld" to match here
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);

and here.
return 0;}

--
Ben.
thanks,thanks
Oct 26 '08 #7
On 10月25日, 下午7时44分, "Bartc" <b...@freeuk.com>wrote:
"questions" <mengqidhu...@yahoo.cnwrote in message

news:4a**********************************@z6g2000p re.googlegroups.com...
Tell me what fault it is ?
when I type 12345,it'll give me "1 2 3 4 5",but when the integer I
type in is too great ,it'll not give me what I want.For example,if I
type 78569,I want to get "7 8 5 6 9",but the result is
wrong.why?????
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);

Your question has been answered. However, you want to find out how to use
loops more. If you wanted more digits, you'd need a lot more typing with
your method. Just one example of many possibilties:

#include<stdio.h>

/* extract n'th digit of x (right-most is #1) */
int gd(int x, int n)
{ int d=1;

if (x<0) x=-x;
while (--n) d*=10;
return x%(d*10)/d; /* Using your method */

}

int main(void)
{ int i,x;
# define ndigits 5

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

for (i=ndigits; i>=1; --i) printf("%d ",gd(x,i));
puts("");

return 0;

}

--
bartc
thank you
Oct 26 '08 #8
questions wrote:
On 10月25日, 下午6时59分, "Richard Seriani" <richard_s...@cox.netwrote:
>"questions" <mengqidhu...@yahoo.cnwrote in message

news:4a**********************************@z6g2000 pre.googlegroups.com...


>>Tell me what fault it is ?
when I type 12345,it'll give me "1 2 3 4 5",but when the integer I
type in is too great ,it'll not give me what I want.For example,if I
type 78569,I want to get "7 8 5 6 9",but the result is
wrong.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;}
Convert 12345 to hex.
How many bytes do you need to store that number?
Convert 78569 to hex.
How many bytes do you need to store that number?

How many bytes in an int?

Richard- 隐藏被引用文字 -

- 显示引用的文字 -

5 bytes
How did you arrive at that conclusion?

It's perfectly possible for the size of an int to be 5 bytes, but
extraordinarily unlikely. Given that your program didn't work as you
intended, it's certainly not the case on your machine.
Oct 26 '08 #9
James Kuyper <ja*********@verizon.netwrites:
questions wrote:
>On 10鏈25鏃, 涓嬪崍6鏃59鍒, "Richard Seriani" <richard_s...@cox.netwrote:
<snip>
>>How many bytes in an int?
<snip>
>5 bytes

How did you arrive at that conclusion?

It's perfectly possible for the size of an int to be 5 bytes, but
extraordinarily unlikely. Given that your program didn't work as you
intended, it's certainly not the case on your machine.
Nit: that can't be deduced as certain. sizeof(int) can be 5 and still
have too few value bits to represent the larger of two numbers
originally presented (as I am sure you know).

--
Ben.
Oct 26 '08 #10
On 10月25日, 下午6时45分, questions <mengqidhu...@yahoo.cnwrote:
Tell me what fault it is ?

when I type 12345,it'll give me "1 2 3 4 5",but when the integer I
type in is too great ,it'll not give me what I want.For example,if I
type 78569,I want to get "7 8 5 6 9",but the result is
wrong.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;}
int ai=12345;
int b1,b2,b3,b4,b5;

b5=ai/10000;
b4=(ai%10000)/1000;
b3=((ai%10000)%1000)/100;

b2=((ai%10000)%1000)%100/10;

b1=((ai%10000)%1000)%100%10/1;

printf("\n%d \t%d \t%d \t%d \t%d\t",b1,b2,b3,b4,b5);
Oct 26 '08 #11
In message
<4a**********************************@z6g2000pre.g ooglegroups.com>,
questions <me**********@yahoo.cnwrites
>Tell me what fault it is ?

when I type 12345,it'll give me "1 2 3 4 5",but when the integer I
type in is too great ,it'll not give me what I want.For example,if I
type 78569,I want to get "7 8 5 6 9",but the result is
wrong.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;}

Even I, as a rank amateur beginner, know this ...... the number that
you're inputting (78569) is larger than "int" on your system (where as
12345 isn't to big). I suspect that "int" on your system contains 8 bits
and can have a maximum of 32767, even *if* you were using "unsigned int"
you could input a maximum value of 65535 before overflow occurred

I think that you might need the data type "long". But of course I may
be wrong. It has been known
--
If one person has delusions, we call them psychotic. If, however, 1.5 billion
people have delusions we must apparently call them a religious group, and
respect their delusionary state.
Oct 26 '08 #12
On October 26, 2008 10:52, in comp.lang.c, Tony Quinn (to**@tqvideo.co.uk)
wrote:
In message
<4a**********************************@z6g2000pre.g ooglegroups.com>,
questions <me**********@yahoo.cnwrites
>>Tell me what fault it is ?

when I type 12345,it'll give me "1 2 3 4 5",but when the integer I
type in is too great ,it'll not give me what I want.For example,if I
type 78569,I want to get "7 8 5 6 9",but the result is
wrong.why?????
[snip]
Even I, as a rank amateur beginner, know this ...... the number that
you're inputting (78569) is larger than "int" on your system (where as
12345 isn't to big). I suspect that "int" on your system contains 8 bits
ITYM 16 bits. 8 bits would only give a range of -128 to 127 (for signed
integer) or 0 - 255 (for unsigned integer)
and can have a maximum of 32767, e
Even *if* you were using "unsigned int"
you could input a maximum value of 65535 before overflow occurred

I think that you might need the data type "long". But of course I may
be wrong. It has been known
--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
Oct 26 '08 #13
In message <a4***************************@TEKSAVVY.COM-Free>, Lew
Pitcher <lp******@teksavvy.comwrites
>On October 26, 2008 10:52, in comp.lang.c, Tony Quinn (to**@tqvideo.co.uk)
wrote:
>In message
<4a**********************************@z6g2000pre. googlegroups.com>,
questions <me**********@yahoo.cnwrites
>>>Tell me what fault it is ?

when I type 12345,it'll give me "1 2 3 4 5",but when the integer I
type in is too great ,it'll not give me what I want.For example,if I
type 78569,I want to get "7 8 5 6 9",but the result is
wrong.why?????
[snip]
>Even I, as a rank amateur beginner, know this ...... the number that
you're inputting (78569) is larger than "int" on your system (where as
12345 isn't to big). I suspect that "int" on your system contains 8 bits

ITYM 16 bits. 8 bits would only give a range of -128 to 127 (for signed
integer) or 0 - 255 (for unsigned integer)
>and can have a maximum of 32767, e
>Even *if* you were using "unsigned int"
you could input a maximum value of 65535 before overflow occurred

I think that you might need the data type "long". But of course I may
be wrong. It has been known
See I told you that I'm a beginner and can be wrong, but my line of
thinking is correct (range overflow), is it not?

--
A 'Project Manager' is someone who, when told that the aircraft in which
he is currently a passenger is about to crash, demands that the pilot
stops all that tiresome struggling-with-the-controls-in-the-cockpit
stuff and instead attends a half-hour-long impact-assessment meeting.
Oct 26 '08 #14
Ben Bacarisse wrote:
James Kuyper <ja*********@verizon.netwrites:
>questions wrote:
>>On 10鏈25鏃, 涓嬪崍6鏃59鍒, "Richard Seriani" <richard_s...@cox.netwrote:
<snip>
>>>How many bytes in an int?
<snip>
>>5 bytes
How did you arrive at that conclusion?

It's perfectly possible for the size of an int to be 5 bytes, but
extraordinarily unlikely. Given that your program didn't work as you
intended, it's certainly not the case on your machine.

Nit: that can't be deduced as certain. sizeof(int) can be 5 and still
have too few value bits to represent the larger of two numbers
originally presented (as I am sure you know).
Yes, I wasn't thinking about that. Demote that possibility from
"certain" to "nearly certain".
Oct 26 '08 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mike | last post by:
Hello All, I'm trying to deploy my windows-based application using crystal report. i package the setup by including the crystal report file, the exe file, and two merge modules for the crystal...
3
by: Mitchell Thomas | last post by:
I hope someone out there can solve my mysterious problem. I have tried everything imaginable, even paid $35 to Microsoft to help me, but they were not able to figure out this problem: Here is the...
1
by: jomichie | last post by:
Plz Help Me I Have a simple datsbase with 2 tables tblproducts productID primary key productname text unit cost currency tblorder orderID productID
8
by: pei_world | last post by:
Hi, there; I have a problem with my datagrid control. I declared it in one of my form, set with DataGridTalbeStyle as well, and when I click on button, I would like to retrive Data from Database...
3
by: Jazzkt | last post by:
Hello, Folks, I am new to VC++ and I just added a form to my application. The GUI has a group of checkboxes, a combo box, a text box and two buttons. Will someone please walk me through...
3
by: Randy2203 | last post by:
I try to log onto runescape, and get go to the website. That works. Then I click on enter an existing user, and that works. Then I click on a world and It goes to like a black page and then nothing...
1
by: bakxchaixDD | last post by:
hi...i really really really need help with this. there is too much to be processed & my brain cannot handle all the arrays that need to written. This is the project: Mobile Phone Contract ...
11
by: tracy | last post by:
Hi, I really need help. I run this script and error message appeal as below: drop trigger log_errors_trig; drop trigger log_errors_trig ERROR at line 1: ORA04080: trigger 'LOG_ERRORS-TRIG'...
0
by: maximoaldea | last post by:
Hi Guys, I need some help with my project. I'm trying to split wave files depending on the playtime e.g. 01:00-02:00 etc. I've search the net entirely and I can't seem to find an item on this....
1
by: roxys | last post by:
Hello, I was wondering if someone could help me a bit here. Im trying to desing an electronic university students enrollement system. The system requirements are as follows: 1.Students can...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.