Arrays sample program in C

By: Jagan  

This is a sample program to count the number of occurrences of each digit, of white space characters (blank, tab, newline), and of all other characters. This is artificial, but it permits us to illustrate several aspects of C in one program.

There are twelve categories of input, so it is convenient to use an array to hold the number of occurrences of each digit, rather than ten individual variables. Here is one version of the program:




   #include <stdio.h>

   /* count digits, white space, others */
       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')
           else if (c == ' ' || c == '\n' || c == '\t')

       printf("digits =");
       for (i = 0; i < 10; ++i)
           printf(" %d", ndigit[i]);
       printf(", white space = %d, other = %d\n",
           nwhite, nother);
The output of this program on itself is
   digits = 9 3 0 0 0 0 0 0 0 1, white space = 123, other = 345
The declaration
   int ndigit[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 decision as to whether a character is a digit, white space, or something else is made with the sequence

   if (c >= '0' && c <= '9')
   else if (c == ' ' || c == '\n' || c == '\t')
The pattern
   if (condition1)
   else if (condition2)
occurs frequently in programs as a way to express a multi-way decision. The conditions are evaluated in order from the top until some condition is satisfied; at that point the corresponding statement part is executed, and the entire construction is finished. (Any statement can be several statements enclosed in braces.) If none of the conditions is satisfied, the statement after the final else is executed if it is present. If the final else and statement are omitted, as in the word count program, no action takes place. There can be any number of

else if(condition)

groups between the initial if and the final else.

As a matter of style, it is advisable to format this construction as we have shown; if each if were indented past the previous else, a long sequence of decisions would march off the right side of the page.

Extracted from the Book written by Kernighan and Ritchie.

Archived Comments

1. 2 d array program info...
View Tutorial          By: bhavi at 2012-06-21 07:01:36

2. naka pagod nito ahh
View Tutorial          By: chellycyl fee at 2012-02-14 06:49:47

3. your nice
View Tutorial          By: rymer at 2011-03-23 01:30:41

4. i am looking for c programs in written
View Tutorial          By: ziyauddin at 2011-02-13 06:21:09

5. helpful 4me.
View Tutorial          By: tikendrajit at 2010-06-15 02:58:54

6. Lifted straight out of Kernighan and Ritchie' Book on C programming without acknowledging the autho
View Tutorial          By: Jason at 2008-08-06 09:20:02

Most Viewed Articles (in C )

Latest Articles (in C)

Comment on this tutorial