Question

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

My question is:

What has changed in C, so that getline() is not valid, but get_line() is a valid name for a 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;
    }
}

The errors I get are:

  • ./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: error: too few arguments to function call, expected 3, have 2 while ((len = getline(line, MAXLINE)) > 0); and

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

Was it helpful?

Solution

The stdio library distributed with glibc declares a function that is also called getline with a different signature than yours. Since you cannot declare two functions with the same name the compiler gives an error. The conflicting declaration of getline found in stdio.h is:

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

The getline function was originally a glibc extension, and has then been included in POSIX.1-2008. It is not a standard C function.

If you are using gcc you can get standards-compliant behavior by using the -std command line switch. Among other things this hides declarations of non-standard functions. Try for example:

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

OTHER TIPS

stdio.h already has a declaration of getline(),

You should not use same name to define your getline function.

If you want to declare and define same function, you should use another name. like get_line()

You get a conflict between your new-declared getline function and the one declared in stdio.h.

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