これは、「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;
}
Ubuntu11.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)と、そのリンクから演習18、19、20、21などを実行しようとすると、同様のエラーが発生します。プログラムを実行して出力を確認できない場合、学習するのは非常に困難です。この問題は、1つのプログラムに文字配列と関数呼び出しを導入したときに始まりました。この問題についてアドバイスをいただければ幸いです。