本文介绍了getch() 在没有 conio.h 的情况下工作 - 这怎么可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我开始使用 Visual Studio 2010 (c++) 用 C 编写代码.

I started using Visual Studio 2010 (c++) to code in C.

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

int main(){
    printf("test");
    getch();
    return 0;
}

即使没有添加 conio.h 库并且程序在那里暂停,这段代码也能工作,但是 getch();下划线表示,Error identifier getch();未定义.

this code works, even without adding conio.h library and the program is paused right there, however getch(); is underlined and it says, that Error identifier getch(); is undefined.

这怎么可能?

推荐答案

C 语言对函数有一个隐式声明"的概念.如果您不提供原型,编译器将假定函数声明如下:

The C language has a concept of "implicit declarations" for functions. If you don't provide a prototype, the compiler will assume the function was declared like:

int getch();

意味着它是一个返回 int 的函数,它不指定有关其参数的信息.严格来说,这不是错误,但是如果您的警告级别设置得足够高,许多编译器会发出警告.从 C99 开始,此行为已被移除,您通常应避免依赖它.

meaning that it is a function returning int that doesn't specify information about its parameters. Strictly speaking this is not an error, but many compilers will issue a warning if your warning level is set high enough. This behavior was removed in C99 onward, and you should generally avoid relying on it.

由于库仍然提供 getch() 函数,因此在链接时解析其符号没有问题.结果,并且由于 getch() 实际上确实返回了 int,所以一切正常.

Since the library still provides the getch() function, there is no issue resolving its symbol at link time. As a result, and due to the fact that getch() actually does return an int, everything works out.

这篇关于getch() 在没有 conio.h 的情况下工作 - 这怎么可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 13:16