c语言getch函数

In this article, we’ll take a look at using the getch() function in C/C++.

在本文中,我们将研究在C / C ++中使用getch()函数。

The getch() function is very useful if you want to read a character input from the keyboard.

如果要从键盘读取字符输入,则getch()函数非常有用。

While this is not a part of the C standard, this is still a POSIX C function. So, we can still use this function from Windows / Linux / Mac.

尽管这不是C标准的一部分,但它仍然是POSIX C函数。 因此,我们仍然可以在Windows / Linux / Mac上使用此功能。

Let’s take a look at using this function, using a few examples.

让我们使用一些示例来看看如何使用此功能。



C / C ++中getch()的基本语法 (Basic Syntax of getch() in C/C++)

This function takes in a single character from the standard input (stdin), and returns an integer.

此函数从标准输入( stdin )接收单个字符,并返回一个整数。

This is there as part of the <conio.h> header file, so you must include it in your program.

这是<conio.h>头文件的一部分,因此您必须将其包括在程序中。


#include <conio.h>
int getch();

This function does not take any parameters.

此函数不带任何参数。

Here, getch() returns the ASCII value of the character read from stdin.

在这里, getch()返回从stdin读取的字符的ASCII值。

For example, if we give the character ‘0’ as input, it will return the ASCII value of ‘0’, which is 49.

例如,如果我们将字符“ 0”作为输入,它将返回ASCII值“ 0”,即49。

Now, in C / C++, we can directly convert a character to an integer. So on typecasting, the ASCII value 49 will be cast to the char value of ‘0’!

现在,在C / C ++中,我们可以将字符直接转换为整数。 因此,在类型转换中,ASCII值49将强制转换为char值'0'!

Let’s now look at some examples.

现在让我们看一些示例。



在C / C ++中使用getch()–一些示例 (Using getch() in C/C++ – Some Examples)

As a simple example, let’s first look at reading a single character.

作为一个简单的示例,让我们首先看一下读取单个字符。


#include <stdio.h>
#include <conio.h>int main() {char ch = getch();printf("Received Input: %c\n", ch);return 0;
}

Sample Output

样本输出


Received Input: a

I got this output, after I typed ‘a’ on my keyboard. Let’s now look at a program, which waits for 5 characters from the keyboard.

在键盘上输入“ a”后,我得到了此输出。 现在让我们看一个程序,该程序等待键盘上的5个字符。

Note that getch() will NOT display the input from the keyboard. So, when you type the input, the cursor won’t show the input.

请注意getch()不会显示键盘输入。 因此,当您键入输入内容时,光标将不会显示输入内容。

Let’s display the complete string only after we get all 5 characters

仅在获得全部5个字符后才显示完整的字符串


#include <stdio.h>
#include <conio.h>int main() {// Set op = {0, 0, 0, 0, 0, 0} = '\0\0\0\0\0\0' stringchar op[6] = {0};for (int i=0; i<5; i++) {op[i] = getch();}printf("Received 5 character Input: %s\n", op);return 0;
}

Output

输出量


Received 5 character Input: Hello

Indeed, when I typed “Hello”, I did get the output correctly.

确实,当我键入“ Hello”时,我确实正确获得了输出。

Notice that I have 6 characters in my output string, since we need to reserve 1 byte for ‘\0’. So op is “Hello\0”.

注意,我的输出字符串中有6个字符,因为我们需要为'\ 0'保留1个字节。 因此op是“ Hello \ 0”。



结论 (Conclusion)

In this article, we learned about using the getch() function in C / C++ to receive character input from the keyboard.

在本文中,我们学习了如何在C / C ++中使用getch()函数来接收来自键盘的字符输入。

For more content on C and C++, do go through our tutorial section on C programming!

有关C和C ++的更多内容,请阅读我们有关C编程的教程部分 !

参考资料 (References)

  • Linux Manual Page on getch() function in CC语言中getch()函数的Linux手册页


翻译自: https://www.journaldev.com/42088/getch-function-in-c-plus-plus

c语言getch函数

c语言getch函数_在C / C ++中使用getch()函数相关推荐

  1. matlab中floor函数,floor函数_怎么在excel中使用floor函数

    floor函数即上取整函数,是计算机C语言中的数学函数,与ceil函数相对应.但是它在excel中却是另一种含义,FLOOR函数是向下舍入为最接近指数基数的倍数,下面小编就教你怎么在excel中使用f ...

  2. python中add函数_如何使用python中的add函数?

    之前向大家介绍过python中的求和函数sum函数,numpy中的sum函数,对于数组可以指定维度进行相加.numpy中还有另一种求和运算方法,即add函数.add函数不仅作用于numpy中加法运算, ...

  3. python中index函数_详解python中的index函数用法

    1.函数的创建 def fun(): #定义 print('hellow') #函数的执行代码 retrun 1 #返回值 fun() #执行函数 2.函数的参数 普通参数 :要按照顺序输入参数 de ...

  4. 原生js已载入就执行函数_手写CommonJS 中的 require函数

    前言 来自于圣松大佬的文章<手写CommonJS 中的 require函数> 什么是 CommonJS ? node.js 的应用采用的commonjs模块规范. 每一个文件就是一个模块, ...

  5. mounted钩子函数_怎样实现Vue中mounted钩子函数获取节点高度

    这次给大家带来怎样实现Vue中mounted钩子函数获取节点高度,实现Vue中mounted钩子函数获取节点高度的注意事项有哪些,下面就是实战案例,一起来看一下. 遇到的问题 最近在开发一个Vue的项 ...

  6. python grid函数_详解numpy中的meshgrid函数用法

    numpy中的meshgrid函数的使用 numpy官方文档meshgrid函数帮助文档https://docs.scipy.org/doc/numpy/reference/generated/num ...

  7. python hasattr函数_浅谈python中的getattr函数 hasattr函数

    hasattr(object, name) 作用:判断对象object是否包含名为name的特性(hasattr是通过调用getattr(ojbect, name)是否抛出异常来实现的). 示例: & ...

  8. python numpy sum函数_如何使用Python中的sum函数?

    之前小编向大家介绍过python中的sum函数(https://www.py.cn/jishu/jichu/22025.html).在python中sunm函数使用分为两种情况,一种是python自带 ...

  9. python中从小到大排序的函数_深入理解Python中的排序函数

    由于 Python2 和 Python3 中的排序函数略有区别,本文以Python3为主. Python 中的排序函数有 sort , sorted 等,这些适用于哪些排序,具体怎么用,今天就来说一说 ...

  10. mysql数据库div函数_关于使用mysql中的div函数报错?报错-问答-阿里云开发者社区-阿里云...

    数据库MySQL 5.5.27 jar包:mysql-connector-java-5.1.21.jar mybatis-spring-1.1.1.jar druid-0.2.10.jar 集成myb ...

最新文章

  1. unix实际用户ID和有效用户ID解析
  2. MapObjects2自带例子的问题
  3. java 接口 this参数_Java BiFunction 接口实例
  4. PHP中文无法查询,php 中htmlentities导致中文无法查询问题
  5. Bleu:此'蓝'非彼蓝
  6. input 禁止 复制 粘贴 剪切 操作
  7. Spark 计算人员二度关系
  8. 计算机核心期刊加拿大,ssci或cssci期刊北京大学图书馆版核心期刊国外学术.doc...
  9. 【阿里云镜像】配置阿里云Maven 镜像
  10. ShaderForge - 纹理水平垂直翻转
  11. 3dmax 注意事项
  12. 国足0:2不敌韩国 淘汰赛将战泰国
  13. 六:分布式架构存储设计
  14. js使用策略模式实现表单验证
  15. hive表信息查询、查看表结构、表操作等
  16. open-set recognition(OSR)开集识别的一些思考(一)
  17. 10万亿同业存款免缴存准 全面降准将推迟
  18. 电容-去耦和退耦的注意事项
  19. 通过U盘实现西门子二代精简触摸屏恢复出厂设置的具体方法
  20. 四旋翼无人机建模与实现(三)

热门文章

  1. 网络编程必读经典书籍
  2. python科学计算之anaconda
  3. NoSQL数据库简单介绍
  4. 百货商场如何运用预付费系统进行电能管理呢?
  5. 长距离大口径输水PCCP管道泄漏监测系统解决方案
  6. Android GSYVideoPlayer视频播放器
  7. 做游戏代理要找游戏源码平台
  8. HTML+CSS——网页设计项目完整版本(阿里矢量图标库)分享
  9. vnpy 量化交易系统 源码分析 v0.01
  10. 虚幻引擎游戏技能系统文档