I am making my way through K&R2 and wrote a small program to test the fputs and fgets functions given in section 7.7. When I compile, I receive error: conflicting types for 'fputs'. I do not receive the same error for 'fgets'. From what I understand, fputs and fgets are both in stdio.h, so why am I only getting the error for fputs?
If I rename fputs to fputs_77, there are no compile errors. Without also renaming fgets, how do I know the call to it (in the getline function) is from the fgets function in my program and not stdio.h?
I compiled with:
gcc -Wall -Wextra -ansi -pedantic -fno-builtin 7.7.c -o 7.7
#include <stdio.h>
#include <string.h>
#define MAXLINE 1000
char *fgets(char *s, int n, FILE *iop);
int fputs(char *s, FILE *iop);
int getline(char *line, int max);
/* demo of functions in 7.7 */
int main(void)
{
char line[MAXLINE];
while (getline(line, MAXLINE) > 0) {
fputs(line, stdout);
}
return 0;
}
/* fgets: get at most n chars from iop */
char *fgets(char *s, int n, FILE *iop)
{
register int c;
register char *cs;
cs = s;
while (--n > 0 && (c = getc(iop)) != EOF) {
if ((*cs++ = c) == '\n') {
break;
}
}
*cs = '\0';
return (c == EOF && cs == s) ? NULL : s;
}
/* fputs: put string s on file iop */
int fputs(char *s, FILE *iop)
{
int c;
while ((c = *s++)) { /* assignment */
putc(c, iop);
}
return ferror(iop) ? EOF : 0;
}
/* getline: read a line, return length */
int getline(char *line, int max)
{
if (fgets(line, max, stdin) == NULL) {
return 0;
} else {
return strlen(line);
}
}