i am reading the C program book written by Denis and i was practicing his examples. I tried this example on my own and then copy pasted the same example from book. I get the followin error.
Code :
#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 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;
}
Error:
ex16.c:4:5: error: conflicting types for 'getline'
int getline(char line[], int maxline);
^
/usr/include/stdio.h:440:9: note: previous declaration is here
ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict) __OSX_AVAILABLE_...
^
ex16.c:8:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main()
^~~~
ex16.c:16:40: error: too few arguments to function call, expected 3, have 2
while ((len = getline(line, MAXLINE)) > 0)
~~~~~~~ ^
/usr/include/stdio.h:440:1: note: 'getline' declared here
ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict) __OSX_AVAILABLE_...
^
ex16.c:27:5: error: conflicting types for 'getline'
int getline(char s[], int lim)
^
/usr/include/stdio.h:440:9: note: previous declaration is here
ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict) __OSX_AVAILABLE_...
^
1 warning and 3 errors generated.
I am using function prototype correctly only i guess. Refered other internet sources also. I am not sure if its because of compiler. I am using Gcc version 4.3 i guess. OS - mac maverics.
Can you please help me ?
Thanks.
-std=c89for programs from this book.