Question

Go ahead and look to the bottom of this post for the actual question

I may be trying to understand the really simple things a bit too much, but I just cannot continue until I figure this out.

While reading K&R 1.9 Character Arrays, you come across an example program which takes input and determines which line was the longest, then it reprints it.

In order to understand the getline function of this program, I have modified it slightly to print some information during its progression. Now I have found some input which, once I understand, should greatly enhance my understanding of getchar().


On a side note, when I try to read source code from the libraries I get completely overwhelmed. For example I tried hunting down getchar() and ended up staring dumbfounded at a tower of 18 #endif somewhere on line 300ish of stddef.h. I am going to save that headache by discontinuing the pursuit of functions in stdio.h for the near future. Is it normal that the libraries are extremely hard to find something? Shouldn't the function exist as readable source, or do I just have my hopes up?


I will just go ahead and post the entire modified program from K&R for convenience sake:

#include <stdio.h>
#define MAXLINE 5   /* maximum input line length */

int getline(char line[], int maxline);
void copy(char to[], char from[]);

/* print the longest input line */
main()
{
    int len;            /* current line length */
    int max;            /* maximum length seen so far */
    char line[MAXLINE];    /* current input line */
    char longest[MAXLINE]; /* longest line saved here */
    max = 0;
    while ((len = getline(line, MAXLINE)) > 0)
        if (len > max) {
            max = len;
            copy(longest, line);
        }
    if (max > 0)  /* there was a line */
        printf("%s\n", longest);
        printf("INFO: max = %d\n", max);
        printf("INFO: sizeof s[] = %d", sizeof longest);
    return 0;
}

/* getline:  read a line into s, return length  */
int getline(char s[],int lim)
{
    int c, i;
    for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i){
        s[i] = c;
        printf("INFO: s[%d] = %c\n", i, c);
    if (c == '\n') {
        printf("INFO: %d\n", i);
        printf("INFO: s[%d] = \\n\n", i);
        s[i] = c;
        ++i;}
    }
    s[i] = '\0';
    return i;
}

/* copy:  copy ’from’ into ’to’; assume to is big enough */
void copy(char to[], char from[])
{
    int i;
    i = 0;
    while ((to[i] = from[i]) != '\0')
        ++i;
}

Here is some example input/output: I am using CodeBlocks and Windows 7

sampleIO

Finally, my question is why does the for loop in the getline() function leave the loop after it gets to F and therefore places the null terminator after F as \0

My hypothesis is that perhaps putting the EOF character shown as ^Z in the windows terminal, the array is truncated to a length of 4 characters plus a null terminator. It turns out if I put the EOF character inside a line to be read, the saved line s[] will only contain a maximum size of 5. What also confuses me is that the variable len will always be 0, is that because len is assigned during the while loop in main(), and does not have its value saved once that while loop is broken?

Something interesting to me, but probably not to experienced C users, is that the if statement right after the for loop in getline() can either be inside the for loop, or outside and it does not change the program. Interesting, because won't that mean that whether or not the for loop executes any code in the body, at the very least (c=getchar()) is executed?

Sorry the question is so wordy, I appreciate any helpful comments/answers.

Was it helpful?

Solution

Because MAXLINE is 5, and the getline function never reads more than MAXLINE - 1 characters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top