这是一个我试图直接从"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 …Run Code Online (Sandbox Code Playgroud) 是否符合ANSI C标准的实现允许在其标准库中包含其他类型和函数,超出标准列举的那些类型和函数?(理想的答案将参考ANSI标准的相关部分.)
我特别要求,因为Mac OS 10.7 getline在stdio.h中声明了该函数,即使使用该-ansi标志使用gcc或clang进行编译也是如此.这打破了几个定义自己getline功能的旧程序.这是Mac OS 10.7的错吗?(getlineMac OS 10.7上的手册页说明getline符合2008年发布的POSIX.1标准.)
编辑:为了澄清,我发现奇怪的是,在Mac OS 10.7上的ANSI C89程序中包含stdio.h也会引入该getline函数的声明,因为getline它不是K&R(可能是ANSI)中所列举的函数之一. stdio.h中.特别是,尝试编译noweb:
gcc -ansi -pedantic -c -o notangle.o notangle.c
In file included from notangle.nw:28:
getline.h:4: error: conflicting types for ‘getline’
/usr/include/stdio.h:449: error: previous declaration of ‘getline’ was here
Run Code Online (Sandbox Code Playgroud)
它是否是Mac OS 10.7中的一个错误,getline即使在编译ANSI C89标准时也包括stdio.h中的声明?
我正在使用K&R,使用Clang作为我的编译器.
使用Clang编译时,练习1-16会产生"getline'的冲突类型"错误.我猜是因为其中一个默认库有一个getline函数.
在编译K&R练习时,我应该向Clang传递哪些选项以避免包含任何其他内容?
要修改的运动样本是:
#include <stdio.h>
#define MAXLINE 1000
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 line lenght 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); …Run Code Online (Sandbox Code Playgroud)