Section 1.6 Arrays


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


This section introduces arrays. Arrays in C hold a number of same typed variables into a one entity and are indexed by their position. In this program it is demonstrated by holding the count of number of digits in the array int ndigit[10]; This program lets us count the digits, whitespace and others. There are 10 digits, ranging from 0 to 9, so we create a array, ndigits which can hold 10 digits. In the program we getchar() and for characters between ‘0’ and ‘9’, we take it and substract ‘0’ from it so that we can get the value and we increment array index at that value.

In the end, we print the values stored in the array.