rubing Posted February 7, 2009 Share Posted February 7, 2009 I have the following code that determines the number of digits (0, 1, 2, 3, etc..), whitespace, and characters in a file. I don't understand why the line in red (++ndigit[c-'0'] can't be written merely as ++ndigit[c]? Thanks! #include <stdio.h> main() { int c,i,nwhite,nother; int ndigit[10]; nwhite=nother=0; for (i=0; i<10;++i) ndigit[i]=0; while ((c=getchar())!=EOF) { if (c>='0' && c<='9') [b][color=red]++ndigit[c-'0'];[/color][/b] else if (c == ' ' || c=='\n' || c=='\t') ++nwhite; else ++nother; } printf("digits ="); for (i=0; i<10; ++i) printf(" %d",ndigit[i]); printf(",white space=%d,other=%d\n",nwhite,nother); } Quote Link to comment https://forums.phpfreaks.com/topic/144158-solved-numerical-values-and-characters-in-c/ Share on other sites More sharing options...
corbin Posted February 7, 2009 Share Posted February 7, 2009 Oh, it just now occurred to me why EOF was used here and in your other post. I never would've dreamed a noobie book would have you piping a file (or steam of somekind) into a program. Pretty interesting though. Anyway, as I mentioned in the other thread, this is another use of the ASCII table. On the ASCII table (http://www.asciitable.com/), the numbers are 48-57 (decimal). So, if the character value (on the ASCII table) is >= 48 and <= 57, it's a decimal. Then, the -'0' is actually equivalent to -48. So, if it were 1 for example, the ASCII value would be 49. 49-48=1. A 4 byte character is essentially an integer, just displayed as a character. For example, do: sprintf("%i %c", ((int) '0'), (char) 48); And it should output "48 0". ;p Quote Link to comment https://forums.phpfreaks.com/topic/144158-solved-numerical-values-and-characters-in-c/#findComment-756559 Share on other sites More sharing options...
rubing Posted February 7, 2009 Author Share Posted February 7, 2009 ooooh...that's tricky! but I get what your saying. We're just converting the ascii value to actual integer value. It also took me a while to figure out that EOF line, but I did pipe some file into it and it worked. This is one of the classic texts on C programming so, its surprising that wasn't made clearer. Quote Link to comment https://forums.phpfreaks.com/topic/144158-solved-numerical-values-and-characters-in-c/#findComment-757032 Share on other sites More sharing options...
corbin Posted February 7, 2009 Share Posted February 7, 2009 I'm quite surprised they expected you to know to pipe a file in these basic examples. Pretty weird. I guess they assumed we would see EOF and go "File? Hrmmm.... Guess we have to pipe one." lol. Or maybe it says something at the beginning of the book or something. I don't know. (Technically EOF matches the end of any stream, since it's simply -1, and -1 is returned when nothing can be read, so maybe they expected you to use it some other way. Not sure,) Quote Link to comment https://forums.phpfreaks.com/topic/144158-solved-numerical-values-and-characters-in-c/#findComment-757042 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.