《C程序设计语言》示例报错“conflicting types for ‘getline’”勘误

本文记录了使用《C程序设计语言》一书中示例代码时遇到的编译错误,特别是与标准库函数getline类型的冲突问题,并提供了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     手动跑C语言“圣经”《C程序设计语言》(https://book.douban.com/subject/1139336/)自带的例子(P21页)时,编译竟然报错,一脸懵逼。

1_9.c:7:5: error: conflicting types for ‘getline’
 int getline(char line[], int maxline);
     ^
In file included from 1_9.c:1:0:
/usr/include/stdio.h:678:20: note: previous declaration of ‘getline’ was here
 extern _IO_ssize_t getline (char **__restrict __lineptr,
                    ^
1_9.c:31:5: error: conflicting types for ‘getline’
 int getline(char s[], int lim)
     ^
In file included from 1_9.c:1:0:
/usr/include/stdio.h:678:20: note: previous declaration of ‘getline’ was here
 extern _IO_ssize_t getline (char **__restrict __lineptr,
                    ^

     看一下报错,是和“ /usr/include/ ”下的C标准库“ stdio.h ”有冲突,该文件也声明定义了“ getline ”。

     所以,把原书示例中的“ getline ”函数换一下名字就OK了,编译成功。书的内容是有点老了!

root@ubuntu:~/c# gcc -o 1_9 1_9.c 
root@ubuntu:~/c# ./1_9 < 1_9.c 
	for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值