C语言 printf 未在此范围内声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38560419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printf was not declared in this scope
提问by AV 198
回答by R Sahu
The book is outdated.
这本书已经过时了。
Add
添加
#include <stdio.h>
to your program. It provides the declaration of printfand many other functions. For a complete list of functions defined in the file, lookup its documentation.
到你的程序。它提供了printf许多其他功能的声明。有关文件中定义的函数的完整列表,请查找其文档。
回答by abhiarora
The compiler didn't find declaration for printffunction. That's why it shows compilation error.
编译器没有找到printf函数的声明。这就是它显示编译错误的原因。
The correct declaration (ISO/IEC 9899:1999) of printffunction is:
功能的正确声明 (ISO/IEC 9899:1999)printf是:
int printf(const char * restrictformat, ... );
You can either declare the function like above before calling it or you can include header filewhich contains declaration of that function.
But it would be easiest and safest to just include the header file which contains declaration of your function (#include <stdio.h>for printf).
您可以在调用之前像上面那样声明函数,也可以包含header filewhich 包含该函数的声明。但是,只包含包含函数声明的头文件(#include <stdio.h>for printf)将是最简单和最安全的。
If you want to know why you need to supply declaration of the function before calling it, you can have a look at this question. The explanation is given below-
如果你想知道为什么你需要在调用之前提供函数的声明,你可以看看这个问题。解释如下——
The C programming language was designed so that the compiler could be implemented as a one-pass compiler. In such a compiler, each compilation phase is only executed once. In such a compiler you cannot referrer to an entity that is defined later in the source file.
C 编程语言的设计使编译器可以实现为一次性编译器。在这样的编译器中,每个编译阶段只执行一次。在这样的编译器中,您不能引用稍后在源文件中定义的实体。
Moreover, in C, the compiler only interpret a single compilation unit (generally a .c file and all the included .h files) at a time. So you needed a mechanism to referrer to a function defined in another compilation unit. All identifiers in C need to be declared before they are used. This is true for functions as well as variables. For functions the declaration needs to be before the first call of the function. A full declaration includes the return type and the number and type of the arguments. This is also called the function prototype.
此外,在 C 中,编译器一次只解释一个编译单元(通常是一个 .c 文件和所有包含的 .h 文件)。因此,您需要一种机制来引用另一个编译单元中定义的函数。C 中的所有标识符都需要在使用之前声明。对于函数和变量都是如此。对于函数,声明需要在第一次调用函数之前。完整的声明包括返回类型以及参数的数量和类型。这也称为函数原型。
You can also define a function before calling it in the same compilation unit. Or you can just declare it before calling it. It is better idea (not always) to include the header file which contains the declaration of the function.
您还可以在同一编译单元中调用之前定义一个函数。或者你可以在调用它之前声明它。最好(并非总是)包含包含函数声明的头文件。
and consider buying a new book. The author should have mentioned the header file inclusion.
并考虑买一本新书。作者应该提到头文件包含。
回答by CinCout
printf()is declared in stdio.hheader file.
printf()在stdio.h头文件中声明。
Add this as the first line of your program:
将此添加为程序的第一行:
#include <stdio.h>
回答by Vijay S B
Add a
添加一个
#include<stdio.h>
at the beginning of .c file. It is a header file to be included for every c program you write. stdio.h header file contains standard input/output function declaration.(eg. printf and scanf)
在 .c 文件的开头。它是您编写的每个 c 程序都包含的头文件。stdio.h 头文件包含标准输入/输出函数声明。(例如 printf 和 scanf)
回答by Naresh Teli
You have to include header file :"stdio.h"
您必须包含头文件:“stdio.h”


