无法编译《C 编程语言》第二版第 29 页上的代码。
我变了 'getline
' to 'GetLine
' 在代码中使其工作。
我们怎样做才能让程序在不改变的情况下编译呢?
作者是如何编译代码的?
这是代码:
#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;
}
问题是getline
全小写已成为实际的POSIX C 函数因此该名称不能在包含以下内容的 POSIX 兼容程序中使用<std*
标头。
您决定将其重命名为GetLine
由于 C 区分大小写,因此避免了这个问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)