I have a question regarding the getline () function and parameter definition in the following code. The code is taken directly from K & R chapter 1.9: "Arrays of characters." I reproduced it here verbatim. The problem is that when I compile the program, I get three errors (which I reproduced at the end). When I change the function and the parameter definition of the get_line () function (with an underscore, not just getline) in three places where I get errors, the errors stop and the program runs as expected.
My question is:
What has changed in C so getline () is not valid, but get_line () is a valid name for the function definition?
#include <stdio.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
void copy(char to[], char from[]);
int 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;
}
}
I get the following errors:
./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: : , 3, 2
while ((len = getline(line, MAXLINE)) > 0);
./section 1.9.1.c: 30: 5: : 'getline'
int getline(int s[], int lim)