GCC警告:C99中函数'puts'的隐式声明无效

gro*_*k12 30 macos gcc

我开始Zed Shaw的学习C艰难的方式.我已经下载了XCode和命令行工具.但是当我编译第一个程序时:

int main(int argc, char *argv[]) {
     puts("Hello world."); 
     return 0;
 }
Run Code Online (Sandbox Code Playgroud)

我收到这个警告:

ex1.c:2:1:警告:C99中函数'puts'的隐式声明无效[-Wimplicit-function-declaration]

该程序可以正确编译和执行.

我正在使用OSX 10.8.3.输入'gcc -v'给出:

使用内置规格.目标:i686-apple-darwin11配置:/private/var/tmp/llvmgcc42/llvmgcc42-2336.11~182/src/configure --disable-checking --enable-werror --prefix =/Applications/Xcode.app/Contents /Developer/usr/llvm-gcc-4.2 --mandir =/share/man --enable-languages = c,objc,c ++,obj-c ++ --program-prefix = llvm- --program-transform-name =/^ [cg] [^ .-]*$/s/$/ - 4.2/--with-slibdir =/usr/lib --build = i686-apple-darwin11 --enable-llvm =/private/var/tmp /llvmgcc42/llvmgcc42-2336.11~182/dst-llvmCore/Developer/usr/local --program-prefix = i686-apple-darwin11- --host = x86_64-apple-darwin11 --target = i686-apple-darwin11 - with-gxx-include-dir =/usr/include/c ++/4.2.1线程模型:posix gcc版本4.2.1(基于Apple Inc. build 5658)(LLVM build 2336.11.00)

请帮忙.

and*_*wmu 44

你需要包括stdio.h,即

#include <stdio.h> 
Run Code Online (Sandbox Code Playgroud)

在开始导入函数定义.

  • 同样在第2课中,Zed指出您可以使用include语句来消除警告.所以我给你正确答案. (2认同)

Ant*_*ala 5

应该将这本"书"重新命名为通过遵循明显错误的无意义的例子学习仇恨C.

现代C中的正确代码简单明了

#include <stdio.h>        // include the correct header

int main(void) {          // no need to repeat the argument mantra as they're not used
    puts("Hello world."); 
}                         // omit the return in main as it defaults to 0 anyway
Run Code Online (Sandbox Code Playgroud)

而原始的例子

int main(int argc, char *argv[]) {
    puts("Hello world."); 
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在1989 (即在写这个答案之前18年,并且几乎在写"书"之前的几年)中,C标准进行了修订,这本来就是一件坏事.在C99修订版中,这种隐含的函数声明被认为是非法的 - 当然它在当前的标准修订版(C11)中仍然是非法的.因此,使用荷兰国际集团的相关报头,即,预先考虑(或声明与功能)是约束错误.puts #include#include <stdio.h>putsint puts(const char*);

约束错误是必须导致编译器输出诊断消息的错误.另外,这样的程序被认为是无效的程序.然而,关于C标准的特殊之处在于它允许C编译器也成功编译无效程序,尽管编译器也可以拒绝它.因此,这样的例子在一本本应向初学者教授C的书中几乎不是一个好的起点.