K & R 2nd Edition Example 1.9 Character Arrays

I have a question regarding the getline () function and parameter definition in the following code. The code is taken directly from K & R chapter 1.9: "Arrays of characters." I reproduced it here verbatim. The problem is that when I compile the program, I get three errors (which I reproduced at the end). When I change the function and the parameter definition of the get_line () function (with an underscore, not just getline) in three places where I get errors, the errors stop and the program runs as expected.

My question is:

What has changed in C so getline () is not valid, but get_line () is a valid name for the function definition?

#include <stdio.h>
#define MAXLINE 1000    // maximum input line size

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

/* print longest input line */

int main()
{
    int len;            //current line lenght
    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", 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;
    if (c == '\n') {
        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;
    }
}

I get the following errors:

  • ./section 1.9.1.c: 4: 5: error: conflicting types for 'getline'; int getline(int line[], int maxline);

  • ./section 1.9.1.c: 17: 40: : , 3, 2 while ((len = getline(line, MAXLINE)) > 0);

  • ./section 1.9.1.c: 30: 5: : 'getline' int getline(int s[], int lim)

+4
3

stdio, glibc, , getline , . , . getline, stdio.h, :

   ssize_t getline(char **lineptr, size_t *n, FILE *stream);

getline glibc, POSIX.1-2008. C.

gcc, , -std. , . , :

gcc -Wall -pedantic -std=c11 "section 1.9.1.c" -o "section 1.9.1"
+3

stdio.h getline(),

getline.

, . get_line()

+3

getline stdio.h.

+2

All Articles