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函数

### 回答1: getch()函数C语言的一个库函数,属于ncurses库。它用于从键盘读取一个字符,但不回显在屏幕上。使用方法如下: 1. 包含头文件:在C程序包含<conio.h>头文件。 2. 调用getch()函数使用getch()函数读取键盘输入,该函数将立即返回读取到的字符。例如: char ch = getch(); 注意:getch()函数仅在Windows环境可用,在Linux或其他平台不能使用。 ### 回答2: getch()函数C语言的一个函数,用于从键盘获取一个字符,并将其返回给调用者。该函数通常用于在控制台程序获取用户输入的字符,以供程序做相应的处理。 使用getch()函数需要在程序包含头文件<conio.h>。具体的使用步骤如下: 1. 在程序包含头文件<conio.h>,以便能够调用getch()函数。 2. 在需要获取用户输入的地方调用getch()函数,将其返回的字符存储到一个变量。 3. 对获取的字符做相应的处理,如判断字符是否满足某个条件,然后执行相应的逻辑。 下面是一个简单的示例代码,演示了getch()函数使用: #include <stdio.h> #include <conio.h> int main() { char ch; printf("请输入一个字符:"); ch = getch(); printf("您输入的字符是:%c\n", ch); return 0; } 在上述代码,首先包含了<conio.h>头文件,然后声明了一个字符变量ch。在获取字符的地方调用了getch()函数,并将返回的字符赋值给ch变量。最后,通过printf函数将获取的字符输出到控制台。 需要注意的是,getch()函数在不同的编译器和操作系统上可能实现不同,因此在某些环境可能无法使用。另外,getch()函数在获取字符后,字符是不会显示在屏幕上的,这与一般的输入方式有所不同。 ### 回答3: getch()函数C语言的一个输入函数,用于从终端获取一个字符输入,并将其作为返回值返回。 要使用getch()函数,首先需要包含相应的头文件: ```c #include <conio.h> ``` 然后就可以在程序调用getch()函数了。getch()函数不需要任何参数,使用它可以接收按键输入。例如,可以用下面的代码来获取用户按下的按键并打印出来: ```c #include <stdio.h> #include <conio.h> int main() { char ch; ch = getch(); // 调用getch()函数,获取一个字符输入 printf("您按下了:%c\n", ch); // 打印出获取的字符 return 0; } ``` 当程序执行到getch()函数时,会等待用户在终端按下一个键,然后将该键的ASCII码值作为getch()函数的返回值。在上面的例子,getch()函数获取到的字符会赋值给变量ch,并通过printf函数打印出来。 需要注意的是,由于getch()函数是非标准函数,所以在某些编译器或平台上可能无法使用。此时,可以考虑使用标准库函数getchar()来代替getch()函数,其使用方法类似。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值