fflush(标准输入)ANSI C [英] fflush(stdin) ANSI C
问题描述
我在ANSI C的初学者,我有一个问题,它可能是愚蠢的问题,我很为它难过。
I am a beginner in ANSI C, and I have a question, it may be silly question and I am sorry for it.
#include<stdio.h>
main()
{
int age;
printf("Hello World!\n");
printf("Please enter your age: ");
scanf("%d", &age);
printf("You entered %d\n", age);
fflush(stdin);
getchar();
}
这是我的第二个节目来学习scanf函数的功能。
我的问题是:
我知道,的printf scanf函数 fflush 标准输入和的getchar 的定义在 stdio.h中,但只有当我使用 fflush(标准输入)我必须把#包括LT&; stdio.h中> ,但使用其他方法时,我可以删除该行的#include
It is my second program to learn scanf function. My question is : I know that printf, scanf, fflush, stdin, and getchar are defined in stdio.h but only when I use fflush(stdin) I must put #include<stdio.h>, but when use any other method I can remove that line #include.
推荐答案
您的必须的有无的#include&LT; stdio.h中&GT;
当你调用在头部声明的任何功能。
You must have #include <stdio.h>
when you call any function declared in that header.
好吧,那不是的非常的事实。在1989/1990版本的语言标准,一个电话可以创建一个函数的隐式声明。如果发生这种情况,以配合正确的声明,你可以逃脱它;.否则,你的程序的行为是不明确的 - 这意味着你仍然可能的摆脱它,或没有,但不是必需的编译器警告你一下吧。由于的printf
采用可变数量的参数,你的必须的有一个明显的声明,以避免不确定的行为 - 而获得可见的申报方式是的#include&LT;文件stdio.h方式&gt;
Ok, that's not quite true. In the 1989/1990 version of the language standard, a call can create an implicit declaration of a function. If that happens to match the correct declaration, you can get away with it;. Otherwise your program's behavior is undefined -- which means you still might get away with it, or not, but the compiler isn't required to warn you about it. Since printf
takes a variable number of arguments, you must have a visible declaration to avoid undefined behavior -- and the way to get a visible declaration is #include <stdio.h>
.
(您也可以宣布自己的功能,但是这容易出错,而且也没有充分的理由这样做。)
(You can also declare the function yourself, but that's error-prone, and there's no good reason to do that.)
在C99和以后,没有任何隐含的声明。
In C99 and later, there are no implicit declarations.
的main()
应 INT主要(无效)
。
fflush(标准输入)
有不确定的行为。如果你要放弃 scanf()的
电话后输入的字符,你可以阅读和丢弃它们。
fflush(stdin)
has undefined behavior. If you want to discard characters entered after the scanf()
call, you can read and discard them.
这篇关于fflush(标准输入)ANSI C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!