我正在用 K&R 书学习 C。有一个练习,这里是:“编写一个程序来打印所有超过 80 个字符的输入行”。所以我写了这段代码:
#include <stdio.h>
#include <stdlib.h>
int getline(char s[], int lim);
#define MINLINE 80
#define MAXLINE 1000
/*
*
*/
int main(int argc, char** argv) {
int len; //current line length
char line[MAXLINE]; //current input line
int proceed=0;
while((len=getline(line, MAXLINE))>0)
if(line[len-1]!='\n'){
printf("%s", line);
proceed=1;}
else if(proceed==1){
printf("%s", line);
proceed=0;}
else if(len>MINLINE){
printf("%s", line);
}
return 0;
}
int getline(char s[], int lim){
int i, c;
for(i=0; i<lim-1 && (c=getline())!='*' && c!='\n'; i++){
s[i]=c;
}
if(c=='\n'){
if(i<=lim-1){
s[i]=c;}
i++;}
s[i]='\0';
return i;
}
我无法编译它,也不知道如何修复它。你可以帮帮我吗?
这是错误消息:
main.c:11:5: error: conflicting types for ‘getline’
In file included from /usr/include/stdio.h:62:0,
from main.c:8:
/usr/include/sys/stdio.h:37:9: note: previous declaration of ‘getline’ was here
main.c:38:5: error: conflicting types for ‘getline’
In file included from /usr/include/stdio.h:62:0,
from main.c:8:
/usr/include/sys/stdio.h:37:9: note: previous declaration of ‘getline’ was here
main.c: In function ‘getline’:
main.c:40:5: error: too few arguments to function ‘getline’
main.c:38:5: note: declared here
make[2]: *** [build/Debug/Cygwin_4.x_1-Windows/main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2