Why is there a "conflicting type" error when running this program?

In K & R Chapter 1.9, I experimented with the program below. In particular, what happens if I remove some feature slowdowns.

So, I deleted line # 4. int getline (char line [], int maxline

And the program is perfectly consistent and functioning properly, as far as I know.

When I delete line # 5. void copy (char to [], char from []);

The program produces the following error:

yay.c: 37: 6: warning: conflicting types for 'copy void copy (char to [], char from [])

yay.c: 15: 9: note: previous implicit declaration 'copy here copy (long, line);

#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 = getfatline(line, MAXLINE)) > 0)

        if (len > max) {
            max = len;
            copy(longest, line);
        }

    if (max > 0)
        printf("%s", longest);

return 0;
}


int getfatline(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;
}

Can anyone explain this to me?

+4
3

C C11 ( ) ​​ . , .

, ( ) (- ), ()

  • int
  • .

, , int, .

  • getline() .
  • copy() , .

main() , int . int main(void), , .

+2

Getline stdio.h

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

getline . , , , stdio.h, . , , , .

0

, .

void copy(char to[], char from[]);

, int .


It is for this reason that it works for a function getfatline(), since its return type int, when you use it later in the code, is the same as the default when deleting a prototype.

But when you delete the function prototype copy, the return type is set to by default int, but your function definition has a return type as void, that is, where the conflict occurs, and this gives an error.

0
source

Source: https://habr.com/ru/post/1624987/


All Articles