C语言教程
C语言控制语句
C语言函数
C语言数组
C语言指针
C语言字符串
C语言数学函数
C语言结构
C语言文件处理
C预处理器

C语言getch()

C语言中的getch()是什么?

getch ()是在 conio中定义的预定义非标准函数。 h 头文件。开发人员 C / C++ ,是MS-DOS的编译器,如 Turbo C可以保留屏幕,直到用户传递单个值以退出控制台屏幕为止。它也可以用于从键盘读取单个字节的字符或字符串,然后进行打印。它不包含任何参数。它没有缓冲区来将输入字符存储在程序中。

为什么我们使用getch()函数?

我们使用 getch ()函数可将输出屏幕保持一段时间,直到用户从键盘上传递键以退出控制台屏幕为止。使用getch()函数,我们可以将用户提供的输入字符隐藏在ATM PIN,密码等中。
语法:
int getch(void);
参数: getch()函数不接受用户的任何参数。
返回值: 它返回的ASCII值。
编写一个简单的程序,以显示用户使用C中的getch()函数输入的字符。
程序。 c
#include <stdio.h> 
#include <conio.h> 
int main()
{
       printf(" Passed value by the User is: %c", getch()); // print a character entered by the user
    return 0;
}
输出:
Passed value by the User is: P
要在控制台上打印任何字符,请按相应的键。
编写一个简单的程序以保留控制台屏幕,直到用户提供退出键为止。
Program2.c
#include <stdio.h>
#include <conio.h> 
int main()
{
   printf("Enter a key to exit the console screen.\n");
   getch(); 
   return 0;
}
输出:
Enter a key to exit the console screen.
编写程序以使用getch()函数从用户接受字符串。
Usergetch.c
#include <stdio.h> 
#include <conio.h> 
int main() {    
    char ch[6] = {0}; int x;
    for ( x = 0; x < 5; x++) {
        ch[x] = getch(); // getch() function to take input
    }
    printf("Received 5 character Input: %s\n", ch);
    return 0;
}
输出:
Received 5 character Input: hello
编写程序以使用getch()函数接受来自用户的隐藏密码。
Program3.c
#include<stdio.h>
#include<string.h>
#include<dos.h>
#include<conio.h>
void main()
{
    char pw[10];
    int x;
    printf(" Enter password: ");
    for (x = 0; x < 10; x++)
    {
        // accepts the hidden password using the getch() function
        pw[x] = getch();
        printf("*"); // print the input chartered in the form of *
    }
    pw[x] = '\0';
    printf( "\n" );
printf(" You have passed the hidden password: ");
    for (x = 0; pw[x] != '\0'; x++)
    {
        printf("%c", pw[x]);
}
getch();
}
输出:
Enter password: **********
You have passed the hidden password: Password@1

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4