这是一个我试图直接从"C编程语言"第1.9节运行的程序.
#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 = 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;
}
以下是我try 使用Ubuntu 11.10编译程序时遇到的错误:
cc word.c -o word
word.c:4:5: error: conflicting types for ‘getline’
/usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
word.c:26:5: error: conflicting types for ‘getline’
/usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
make: *** [word] Error 1
为了确保书中的内容没有问题,我参考了书中章节后面练习的这组答案(http://users.powernet.co.uk/eton/kandr2/krx1.html)当我try 从这个链接运行练习18、19、20、21等时,我会遇到类似的错误.当我无法运行程序来查看它们的输出时,学习起来真的很难.这个问题始于在一个程序中引入字符数组和函数调用.如果您能就这个问题提供任何建议,我将不胜感激.