函数隐式声明“残培” [英] implicit declaration of function 'getch'

查看:220
本文介绍了函数隐式声明“残培”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的程序:

#include <stdio.h>
#include <stdlib.h>

int main(void){
    printf("Hello world!\n");

    getch();

    return 0;
}

虽然我得到警告

implicit declaration of function 'getch'

程序运行良好。难道我错过了什么?如果我这样做,为什么程序的工作好吗?

the program runs fine. Do I miss something? And if I do, why does the program work OK?

推荐答案

您收到警告

implicit declaration of function 'getch'

因为你没有包含任何声明头残培。没有这样的功能是在标准头文件中声明&LT; stdio.h中&GT; &LT;文件stdlib.h方式&gt;

because you have not include any header that declares getch. No such function is declared in the standard headers <stdio.h> or <stdlib.h>.

在事实上,在没有命名的功能残培任何的标准C头。

In fact, there is no function named getch in any standard C header.

在此之前的C99标准,C语言允许无可见声明函数调用。这样的呼吁,实际上将创建一个函数的 INT 的任何(晋升)的隐式声明返回,并采取论证键入你真的过去了。

Prior to the C99 standard, the C language permitted calls to functions with no visible declaration. Such a call would in effect create an implicit declaration of a function returning int and taking arguments of whatever (promoted) type you actually passed.

根据这个从来就不是一个好主意。你应该的总是的有一个适当的的#include 指令来声明你在程序中使用的库函数头。

Depending on this has never been a good idea. You should always have a proper #include directive for the header that declares any library function you use in your program.

C99放弃了隐 INT 的规则,并提出给函数的调用没有可见的声明中的约束违反的,需要诊断(该诊断是允许为一个非致命错误。)

C99 dropped the "implicit int" rule and made any call to a function with no visible declaration a constraint violation, requiring a diagnostic (That diagnostic is permitted to be a non-fatal error.)

如果您在Windows编译,如果我没有记错,有一个&LT宣布的getch()功能; CONIO.H&GT; 。如果你想使用该功能,您需要的#include&LT增加; CONIO.H方式&gt; 你的程序

If you're compiling on Windows, if I recall correctly, there's a getch() function declared in <conio.h>. If you want to use that function, you need to add #include <conio.h> to your program.

我不建议这样做;使用残培()是不必要的,使你的程序不可移植。某些Windows开发环境使其难以运行控制台程序(打印到标准输出,而不是创建一个GUI程序);经常跑这样的程序创建的,一旦程序完成摧毁了一个临时窗口。调用标准的getchar()函数是另一种方式,以保持从窗口消失。或者你可以在命令提示符下执行程序,它的输出会出现在当前命令窗口。

I do not recommend doing this; using getch() is unnecessary and makes your program non-portable. Some Windows development environments make it difficult to run "console programs" (programs that print to standard output rather than creating a GUI); often running such a program creates a temporary window that's destroyed as soon as the program finishes. Calling the standard getchar() function is another way to keep the window from vanishing. Or you can execute the program from a command prompt, and its output will appear in your current command window.

如果您是在UNIX系统上编译,有一个名为另一个函数残培(),中声明&LT; curses.h里&GT; 。我可以编译和Linux上执行程序,如果我加入 -lcurses 编译器命令行。但你不应该使用残培()功能,如果你还没有首先设置诅咒的环境,这是相当清楚的,你不想这样做。

If you're compiling on a UNIX-like system, there's another function called getch(), declared in <curses.h>. I can compile and execute your program on Linux if I add -lcurses to the compiler command line. But you shouldn't use that getch() function if you haven't first set up the curses environment, and it's fairly clear you don't want to do that.

在理想情况下,经典的Hello World程序应该只是:

Ideally, the classic "hello world" program should be just:

#include <stdio.h>
int main(void) {
    printf("Hello world!\n");
    return 0;
}

您如何得到运行,让你看到输出取决于环境(你还没有告诉我们)。

How you get that to run and let you see the output depends on your environment (which you haven't told us about).

这篇关于函数隐式声明“残培”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆