0
#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 */
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", longest);
    return 0;
}

/* getline:  read a line int 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;
}

using Mingw and VSCode don't have any additional settings in this post it says POSIX and redefined getline function conflicts. But it works for me why?

K&R Error: conflicting method definition

No Name
  • 1
  • 1
  • Your `` apparently doesn’t declare `getline()` when you compile. You wouldn’t be so lucky on other systems, or perhaps on your system with different compilation options. – Jonathan Leffler Apr 11 '20 at 17:19
  • 1
    i guess this explains it https://stackoverflow.com/a/27381998/13288403 – No Name Apr 11 '20 at 17:21
  • Note that the definition of `main()` is not valid in modern C, meaning C99 or later. It should have an explicit return type of `int`, and preferably `int main(void)`. – Jonathan Leffler Apr 11 '20 at 17:22
  • not valid but works why? – No Name Apr 11 '20 at 17:23
  • Because you are using an old compiler, or because your modern compiler warns you but you’re ignoring it, or … I compile with options so that warnings are errors, and old style function definitions or declarations are rejected as errors. I’d have to fix the code shown before it would compile for me. K&R is great for it’s time, but it’s time was about 30 years ago. C has evolved since then. It no longer illustrates best C practices, or what the C99 or C11 or C18 standards require. – Jonathan Leffler Apr 11 '20 at 17:30
  • yes checked the output section and it actually warns me thanks for answering. – No Name Apr 11 '20 at 17:32

0 Answers0