K&R histogram of length of words I am doing exercise 1-13 of K&amp;R. The problem is to print a histogram of the lengths of words in the input. This is the easier problem, printing the histogram with horizontal bars. This is what I came up with. Is there a better way to do this? Are there any bugs?
#include &lt;stdio.h&gt;

#define MAXLEN 10

int main(){
    int c,length;
    int hist[MAXLEN+1];

    length=0;
    for(int i=0;i&lt;MAXLEN+1;i++) hist[i]=0;

    while((c=getchar())!=EOF){
        if((c==' ' || c=='\t' || c=='\n') &amp;&amp; length){
            if(length&gt;MAXLEN)
                hist[MAXLEN]++;
            else
                hist[length-1]++;
            length=0;
        }
        else if(c!=' ' &amp;&amp; c!='\t' &amp;&amp; c!='\n')
            length++;
    }
    //to deal with situations in which input doesn't end in a space or newline or tab
    if(length&gt;MAXLEN)
        hist[MAXLEN]++;
    else if(length)
        hist[length-1]++;

    //printing the histogram horizontally
    putchar('\n');
    for(int i=0;i&lt;MAXLEN;i++){
        printf(&quot;%3d|&quot;,i+1);
        for(int j=0;j&lt;hist[i];j++) putchar('x');
        putchar('\n');
    }
    printf(&quot;&gt;%d|&quot;,MAXLEN);
    for(int i=0;i&lt;hist[MAXLEN];i++) putchar('x');
    putchar('\n');
}
Declare and initialize variables in one go
You can initialize a value at the same time you declare it. This even works for structs and arrays. For example:
int main() {   
    int c = 0;
    int length = 0;
    int hist[MAXLEN+1] = {0}; // Initializes all elements to 0

    while ((c = getchar()) != EOF) {
        ...

Use isspace()
Instead of manually checking against spaces, tabs and newlines, use isspace() from &lt;ctype.h&gt;.
Split your code into multiple functions
Even for a simple program like this, it is a good exercise to split the code into multiple functions, and reduce the responsibility of each function as much as possible. This way for example, main() could be reduced to:
int main() {
    int hist[MAXLEN + 1] = {0};

    build_histogram(hist, MAXLEN);
    print_histogram(hist, MAXLEN);
}

Then build_histogram() could be written like this:
static void build_histogram(int hist[], size_t hist_size) {
    int c;
    int length = 0;

    while ((c=getchar())!=EOF) {
        if (isspace(c)) {
            add_word(hist, hist_size, length);
            length = 0;
        } else {
            length++;
        }
    }

    add_word(hist, hist_size, length);
}

So its main function is to scan for words. Then add_word() does the actual work of recording the length of a word in the histogram:
static void add_word(int hist[], size_t hist_size, size_t length) {
    if (length &gt; hist_size)
        hist[hist_size]++;
    else if (length)
        hist[length - 1]++;
}

You can use a similar approach for print_histogram().