Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1052519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:06:22+00:00 2026-05-16T17:06:22+00:00

I have just started reading through The C Programming Language and I am having

  • 0

I have just started reading through The C Programming Language and I am having trouble understanding one part. Here is an excerpt from page 24:

#include<stdio.h>

/*countdigits,whitespace,others*/

main()
{
  intc,i,nwhite,nother;
  intndigit[10];
  
  nwhite=nother=0;
  for(i=0;i<10;++i)
      ndigit[i]=0;
      
  while((c=getchar())!=EOF)
      if(c>='0'&&c<='9')
          ++ndigit[c-'0']; //THIS IS THE LINE I AM WONDERING ABOUT
      else if(c==''||c=='\n'||c=='\t')
          ++nwhite;
      else
          ++nother;
          
  printf("digits=");
  for(i=0;i<10;++i)
      printf("%d",ndigit[i]);
  printf(",whitespace=%d,other=%d\n",
      nwhite,nother);
}

The output of this program run on itself is

digits=9300000001,whitespace=123,other=345

The declaration

intndigit[10];

declares ndigit to be an array of 10 integers. Array subscripts always start at zero in C, so the elements are

ndigit[0], ndigit[ 1], ..., ndigit[9]

This is reflected in the for loops that initialize and print the array. A subscript can be any integer expression, which includes integer variables like i,and integer constants. This particular program relies on the properties of the character representation of the digits. For example, the test

if(c>='0'&&c<='9')

determines whether the character in c is a digit. If it is, the numeric value of that digit is

c-'0'`

This works only if ‘0’, ‘1’, …, ‘9’ have consecutive increasing values. Fortunately, this is true for all character sets. By definition, chars are just small integers, so char variables and constants are identical to ints in arithmetic expressions. This is natural and convenient; for example

c-'0'

is an integer expression with a value between 0 and 9 corresponding to the character ‘0’ to ‘9’ stored in c, and thus a valid subscript for the array ndigit.

The part I am having trouble understanding is why the -'0' part is necessary in the expression c-'0'. If a character is a small integer as the author says, and the digit characters correspond to their numeric values, then what is -'0' doing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:06:23+00:00Added an answer on May 16, 2026 at 5:06 pm

    Digit characters don’t correspond to their numeric values. They correspond to their encoding values (in this case, ASCII).

    IIRC, ascii ‘0’ is the value 48. And, luckily for this example and most character sets, the values of ‘0’ through ‘9’ are stored in order in the character set.

    So, subtracting the ASCII value for ‘0’ from any ASCII digit returns its “true” value of 0-9.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started reading a C++ textbook and I am having trouble solving
I have just started learning ruby reading from different resources. One of them is
I have just started reading Modern C++ Design Generic programming and Design Patterns Applied
I have just started learning Objective-C, I am reading Programming in Objective-C 3rd Edition
I have just started reading about ORMLite since I am interested in using it
I have just started on event bubbling in jQuery so my understanding is very
I have just started reading a Hibernate book and it says go to Hibenrate.org
I just started reading this book Eloquent Ruby and I have reached the chapter
I have just started reading about threading in C, using pthreads. I know that
we have just started using a git account of our Django website project so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.