In C language, the getch() function is used to read characters from the stdio stream, that is, reading a character from the console but not displaying it on the screen; the syntax is: "int getch(void)" , returns the characters read.
getch() function in C language
Function: Read characters from the stdio stream, that is, from the console Read a character but not display it on the screen
Usage:
int getchar(void);
This function is a non-echo function. When the user presses a certain character, the function automatically reads it without Press Enter. Some C language command line programs will use this function to make games, but this function is not a standard function, so pay attention to portability!
Getch() under Windows is in the header file conio.h
. To use the getch() function, you need to reference the conio
header file.
Code:
#include <stdio.h> #include <fcntl.h> #include <stdlib.h> #include <conio.h> #define ESC 0x1B #define ENTER 0x0D #define SPACE 0x20 #define KEY_UP 72 //上 #define KEY_DOWN 80 //下 #define KEY_LEFT 75 //左 #define KEY_RIGHT 77 //右 int KEY_EXIT_STATU = 0 ; int KEY_ENTER_STATU = 0 ; int KEY_SPACE_STATU = 0 ; int KEY_UP_STATU = 0 ; int KEY_DOWN_STATU = 0 ; int KEY_LEFT_STATU = 0 ; int KEY_RIGHT_STATU = 0 ; char ch ; int get_value() ; int main(void) { int i = 0; while(1) { get_value(); } return 0 ; } int get_value() { ch = getch() ; system("cls"); switch(ch) { case ESC : KEY_EXIT_STATU = 1 ; printf("退出\n") ; break ; case ENTER :KEY_ENTER_STATU = 1 ; printf("回车\n") ; break ; case SPACE : KEY_SPACE_STATU = 1 ; printf("空格\n") ; break ; case KEY_UP:case 'w' : KEY_UP_STATU = 1 ; printf("上\n") ; break ; case KEY_DOWN:case 's' : KEY_DOWN_STATU = 1 ; printf("下\n") ; break ; case KEY_LEFT:case 'a' : KEY_LEFT_STATU = 1 ; printf("左\n") ; break ; case KEY_RIGHT:case 'd': KEY_RIGHT_STATU = 1 ; printf("右\n") ; break ; } }
Note:
getchar has an int return value. When the program calls getchar, the program waits for the user to press the key and input the user. The characters are stored in the keyboard buffer until the user presses Enter (the Enter character is also placed in the buffer). When the user types Enter, getchar starts reading one character at a time from the stdio stream.
The return value of the getchar function is the ASCII code of the first character entered by the user. If an error occurs, -1 is returned, and the characters entered by the user are echoed to the screen.
If the user presses return If more than one character was entered before, the other characters will be retained in the keyboard buffer, waiting for subsequent getchar calls to read.
That is to say, subsequent getchar calls will not wait for the user to press a key, but directly read the buffer The characters in the buffer area will not wait for the user to press the key until the characters in the buffer area are read.
The basic functions of getch and getchar are the same. The difference is that getch obtains the key value directly from the keyboard without waiting for the user to press Enter. , as long as the user presses a key, getch will return immediately. The return value of getch is the ASCII code entered by the user. If an error occurs, it returns -1. The entered characters will not be echoed on the screen.
The getch function is often used for program debugging During debugging, relevant results are displayed in key positions for viewing, and then the getch function is used to pause the program. When any key is pressed, the program continues to run.
Recommended: "c Language Tutorial》
The above is the detailed content of What is the usage of getch() in C language?. For more information, please follow other related articles on the PHP Chinese website!

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment