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

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' string
    char 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)



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

c语言getch函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值