Why do I get a "conflicting types for getline" error when compiling the longest line example in chapter 1 of K&R2?

why do i keep getting sick every month
why do i keep getting a cold every few weeks
how to get rid of hiccups
why do we get hiccups
what do i need to get a replacement social security card
constantly getting colds
i keep getting sick over and over
i keep getting colds one after another

Here is a program I'm trying to run straight from section 1.9 of "The C Programming Language".

#include <stdio.h>
#define MAXLINE 1000

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

main()
{
    int len;
    int max;
    char line[MAXLINE];
    char longest[MAXLINE];

    max = 0;
    while ((len = getline(line, MAXLINE)) > 0)
        if (len > max) {
        max = len;
        copy(longest, line);
        }
    if (max > 0)
        printf("%s", longest);
return 0;
}


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;
}


void copy(char to[], char from[])
{
    int i;

    i = 0;
    while ((to[i] = from[i]) != '\0')
        ++i;
}

Here is the error I get when I try to compile the program using Ubuntu 11.10:

cc     word.c   -o word
word.c:4:5: error: conflicting types for ‘getline’
/usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
word.c:26:5: error: conflicting types for ‘getline’
/usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
make: *** [word] Error 1

Just to make sure it wasn't a problem with the print in the book, I referenced this set of answers to back of chapter exercises from the book (http://users.powernet.co.uk/eton/kandr2/krx1.html) and I get a similar error when I try to run exercises 18, 19, 20, 21, etc., from that link. It's really hard to learn when I can't run the programs to see how they output. This issue started when introducing character arrays and function calls in one program. I'd appreciate any advice on this issue.

The problem is that getline() is a standard library function. (defined in stdio.h) Your function has the same name and is thus clashing with it.

The solution is to simply change the name.

Why Do I Keep Getting Sick All the Time?, There isn't anyone who hasn't gotten a cold or virus just days before a big event. For some people, being sick is a way of life, and days of feeling well are few� Editor's note: This article was originally published in the February 20, 2018 edition of The Caucus, a publication of LNP Media Group, Inc. Pennsylvania’s decades-old charter school law ranks among

The conflicting function getline() is a GNU/POSIX extension.

K&R state that they address specifically ANSI C in their book (c.f.), which does not provide this function.

The authors present the complete guide to ANSI standard C language programming.

In order set gcc into "K&R compatibility mode" you can specify the ANSI or ISO modes for compilation. These are intended to disable extensions, e.g., the function getline(). This could eventually eliminate the need to edit other examples provided by K&R as well.

For example, the following compile just fine:

$ gcc test.c -ansi
$ gcc test.c -std=c89

(Except that they complain about the implicit default return type of main() with -Wall.)

Apparently on some systems, these modes may not work as presented here (apparently some version(s) of Mac OS fail to correctly disable all extensions). I tested this successfully on my machine:

$ gcc --version
gcc (GCC) 4.7.2 20121109 (Red Hat 4.7.2-8)
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Why Do I Get Tired After Eating? Symptoms of Fatigue, See your doctor if you have these symptoms. You may need medication to treat the infection. Infection Due to a Kidney Stone. You might get chills� Why do some young people die of coronavirus? Get a huge dose and your outcome may be worse. Others argue that genetic susceptibility may be involved: in other words, that there are individuals

This is because the stdio.h have a getline() function.

So a simple thing to make this work would be to rename your function to my_getline()

Both getline() and getdelim() were originally GNU extensions. They were standardized in POSIX.1-2008.

Why Do I Have Cold Chills? 6 Causes of Body Chills Without Fever, Why Do Hiccups Happen? Hiccups can happen for a lot of reasons -- some of them are physical, and some emotional. Why do some people get very sick and even die from Covid-19, while others show few if any symptoms? We know some of the big factors that put people at higher risk of having a severe course of

/usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here

That should give you a hint. Try and rename the getline() function in the code to something else.

Also, declaring main() this way is old style. A function with no declared return type and arguments, by defaults, accepts an unspecified number of arguments and returns an int. This is nearly the case for main(): it does return an int, but has two arguments. You had better declare it as:

int main(int argc, char **argv)

or:

int main(int argc, char *argv[])

Hiccups: Why You Get Hiccups ..and How To Make Them Stop, Acne is a condition of the skin that shows up as different types of bumps. These bumps can be blackheads, whiteheads, pimples, or cysts. Teens get acne� As Americans grapple with the surge of coronavirus infections around the country, the public must prepare now to guard against the upcoming influenza season this fall, writes Richard Webby, a

getline is now a POSIX function declared in stdio.h

Rename you getline function to another name and it will compile.

Why Do I Get Acne? (for Teens), If Get My Payment says your Economic Impact Payment will be issued as a check Below are answers to frequently asked questions related to these Payments. If You Get Coronavirus And Recover, Do You Develop Immunity : Goats and Soda The hope is that a person who has recovered from COVID-19 would have some protection against reinfection in the future

Economic Impact Payment Information Center, Chronic stress or sleep deprivation are factors that can often cause people to keep getting sick. Although simple lifestyle changes are� Despite that statement and apparent fact, Mac computers are still infected by viruses much less often than Windows computers. Below are some of the reasons why Apple Macintosh computers do not have as many viruses as Windows computers: Beginning with macOS X in March 2001, Macintosh operating systems are built on "Darwin," a fork of the BSD kernel.

Why do I keep getting sick? Causes and what to do, Most people feel anxious or scared sometimes, but if it's affecting your life there are things you can try that may help. Support is also available if you're finding it� An expectation is a belief about whether or not you are going to get what you want.. As a psychologist who studies how people create their futures, one of the things I’ve learned is that having

Get help with anxiety, fear or panic, Do I need a REAL ID? You are not required to get a REAL ID, however, starting October 1, 2021, if you typically use your driver license or non-� The blisters usually break and form a sore that crusts over. They will generally heal without treatment within a few weeks.. This article explores why some people get cold sores frequently, as

Comments
  • getline() is not a standard library function, it is an extension, as @moooeeeep stated more in depth and also in here stackoverflow.com/questions/7376566/… by @Dietrich Epp. So, if you are coding today, you shouldn't use getline(), but what if you depend on a library that already uses its self-implemented getline() function? I think, in a broad sense, the @moooeeeep's answer is more appropriate.
  • I wasted 3 beers , and extra 20 minutes and finally gave a thought to google it.
  • I have tried gcc test.c -ansi. It seems that it doesn't work.
  • @Jack what OS/--version did you try?
  • I use macOS Sierra Version 10.12.5. Any substitution for gcc test.c -ansi in this OS?
  • Maybe you could try this: stackoverflow.com/q/7376566/1025391 other than that, you might need to rename the function :(
  • This is the right answer to the question. Not how to fix it, but why does it fail if the book is written by experts? "Because the book was written when getline was not yet standardized." Ahhh, Thank you.
  • @mikiemorales The book was written in compliance with another standard. See my answer: stackoverflow.com/a/17378451/1025391
  • This has been deprecated only in C99. The old style function definition is still used for main function in the last edition of K&R (edition 2).
  • Really? I thought it had been deprecated for as long as the first ANSI C specification...
  • I had the same problem described in the question. I looked up some C references to see whether getline() was in fact defined in the standard library but didn't find it, confusing me even more. The answers I found helpful here were those which specified that getline() is defined in the POSIX Standard C Library. Simply saying 'it already exists' is confusing and probably incorrect.
  • @IsaacKleinman although I admit that my answer isn't precise enought, but still it is correct: quoting from the accepted answer "The problem is that getline() is a standard library function. (defined in stdio.h) "
  • A minor curiosity: FreeBSD provides getline() but does not declare it in any header, exactly to avoid this kind of name clashes (freebsd.org/cgi/…)
  • @loreb That's quite interesting, tho having to #define something just for getline() feels icky.