C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 5th, 2003, 08:10 PM
Alianto Alianto is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 61 Alianto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 2 sec
Reputation Power: 11
getchar() and array

Hi,

Can anyone tell me why I have to use method1 (below) instead of method2 (below)?

method1::
.....
int c;
int digits[10];
.....
while ((c=getchar())!=EOF) {
if (c>='0' && c<='9') {
digits[c-'0']=digits[c-'0']+1;
}
}
.......

method2:
......
int c;
int digits[10];
.....
while ((c=getchar())!=EOF) {
if (c>='0' && c<='9') {
digits[c]=digits[c]+1;
}
}
......

Thanks in advance!

p/s: this program is used to count the occurances of its digits from 0 to 9.

Last edited by Alianto : April 5th, 2003 at 08:15 PM.

Reply With Quote
  #2  
Old April 5th, 2003, 09:45 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 11
because you're dealing with integer numbers and charecters that happen to be numeric representations - there's a big difference between those two things.

0 is zero and '0' is on my computer in decimal is 48 i think. 0 and 48 are very different obviously. say you had '5'. it's decimal value is 53 on my computer (the reason i keep saying my computer is that the values can vary from computer to computer).

53 - 48 = 5

'5' - '0' = 5

so you're converting into the actual values that you want. real numbers. and the reason you don't use '5' - 48 is because of the possible variations on different computers so using '0' makes it much safer - doesn't matter which charecter encoding the computer is using it'll still work out.

Reply With Quote
  #3  
Old April 5th, 2003, 10:06 PM
Alianto Alianto is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 61 Alianto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 2 sec
Reputation Power: 11
I see. Now I know!
B'coz digit[c] is the same as digit[decimal_of_char_c].

Thanks Balance,

Sorry for asking a silly question!

Regards,
Alianto

Reply With Quote
  #4  
Old April 6th, 2003, 01:37 AM
3dfxMM 3dfxMM is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 272 3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 6 Days 17 h 57 m 24 sec
Reputation Power: 17
I am curious what variations in ASCII representations you have seen on different systems.

Reply With Quote
  #5  
Old April 6th, 2003, 06:08 AM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 11
i haven't come accross other char systems myself but apparentlyt hey do exist - so my book tells me. it'd be not called ascii though.

i guess it's pretty rare, but it's quite nice to know that whatever char system you're using '0's going to work.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > getchar() and array

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap