0

So I'm working on learning C by going through The C Programming Language and trying to do the exercises as I go. Exercise 1-16 seems really simple as it's just making an edit to a program that is already given.

What I can't figure out is why I can't get the code from the book, or from any of the online solutions to this exercise to compile... Everything I've tried throws an error that says: "error: conflicting types for ‘getline’"

Here's the original code from the book:

/* Write a program to print all input lines that are longer than 80 characters. */

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

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

int main ()
{

    int len; /* current line length */
    int max; /* maximum line 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;

}

Any glaringly obvious mistakes here? Is it just my machine or how I'm trying to compile it?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 6
    `getline` is part of `stdio.h` – Daniel A. White Aug 03 '20 at 02:22
  • 1
    What @DanielA.White is saying is: change the name of your method `getline()` to something else... like `retrieveline()` -- `getline()` is already defined in `stdio.h` – Andy Aug 03 '20 at 02:27
  • 2
    To expand on the comments by @DanielA.White, on POSIX systems (like e.g. Linux or macOS) there is a [`getline`](https://pubs.opengroup.org/onlinepubs/9699919799//functions/getdelim.html) function already declared in the `` header file. You need to rename your `getline` function to someting else. – Some programmer dude Aug 03 '20 at 02:30
  • Thank you all, that clears things up for me. Any idea why the authors would have written it like that? – Jack de la Motte Aug 03 '20 at 02:33
  • 3
    The book was written before getline() was added to stdio.h. – Jim Rogers Aug 03 '20 at 02:35
  • Ah, makes sense, thanks – Jack de la Motte Aug 03 '20 at 02:37
  • 2
    @JackdelaMotte While the K&R is classic reading, it is not current. Consider using a different book that will get you knowledge of c99 (or better). See https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – dash-o Aug 03 '20 at 03:57
  • Look at the date the book was written. – the busybee Aug 03 '20 at 13:19

0 Answers0