Search code examples
ckernighan-and-ritchie

unexpected behavior of a c program, trying to solve E6-2 from K&R


Exercise 6-2. Write a program that reads a C program and prints in alphabetical order each group of variable names that are identical in the first 6 characters, but different somewhere thereafter. Don't count words within strings and comments. Make 6 a parameter that can be set from the command line. (from K&R)

I started working on the exercise, and I'm stuck on the beginning. I tried to get the whole input and save it line by line in an array, and then point pointers in an array to the saved line. Later I thought of allocating space for each word and coppy them to a tree. However, in the following code when I'm typing as an input:

abc def;'Ctrl-Z'

#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define MAXLINE 100
#define MAXWORDS 10

int read_input(char *[]);

int main(int argc, char *argv[])
{
    char *variables[MAXWORDS] = {0};
    int i;
    int tmp;


    while((tmp = read_input(variables)) != EOF){
        for(i=0; i < tmp; i++){
            printf("%s ",*(variables+i));
        }
    }


    return 0;
}


/* read_input: mapping all the input words into *variables[], and returning the number of the words + 1 */
int read_input(char *variables[]){
    static char s[MAXLINE];
    int tmp;
    int i, j;

    i = 0;
    if((tmp = getline(s, MAXLINE)) == EOF)
        return EOF;

    for(j = 0; j < MAXWORDS && s[i] != '\0'; j++){ /* it does'nt work when j = MAXWORDS because the rest of the line is not saved */
        while(!isalpha(s[i]))
            i++;
        *(variables+j) = &s[i];
        while(isalnum(s[i]) || s[i] == '_')
            i++;
        if(s[i] == '\0')
            continue;
        s[i++] = '\0';
    }
    return j;
}

/* getline: read a line into s, return length */
int getline(char *s,int lim)
{
    static char end; /* static typed char that saves if the last not saved 'c' was an EOF */
    char c;
    int i;
    if(end == EOF){ /* the last input character was an EOF */
        end = 0;
        return EOF;
    }
    for (i=0; i < lim-1 && (c=getchar())!=EOF && c!=';'; ++i)
        s[i] = c;
    s[i] = '\0';
    if(i > 0 && c == EOF)
        end = EOF;
    else if(i == 0 && c == EOF){
        printf("!");
        return EOF;
    }
    else
        return i;

}

the program doesn't stop immediately, and the main's while loop would ask for another input... what am I doing wrong?


Solution

  • Your doing nothing wrong. Due to the usual design of the C stdio library functions, EOF is only recognized if there is no preceding unread input, so you have to type Enter Ctrl-Z or Ctrl-ZCtrl-Z.