Jump to content

Recommended Posts

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);
}

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

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.

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,)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.