3 Ways How to Pause a Program and Press Any Key to Resume

There are three common ways to make a program pause during runtime debugging and press any key to continue:

1,getch();

The header file: conio.h
(conio is the abbreviation of Console Input/Output (console input and output), not the header file in the C standard library, which defines the functions for data input and data output through the console, mainly some The corresponding operations generated by the user by pressing the keyboard, such as the getch() function, etc., the conio library is not only suitable for the Windows platform, but also can be used under Linux. There is already a compatibility package on the Internet, and it can be used after downloading. Windows has no difference, you can use it directly.)
Function purpose: Read a character from the console, but not display it on the screen, wait for input to achieve a pause effect.
Function prototype: int getch(void)
Return value: read character
Use getch(); will wait for you to press any key, and then continue to execute the following statement;
use ch=getch(); will wait for you to press any key after , assign the ASCII code corresponding to the key character to ch, and then execute the following statement.
. On different platforms, enter Enter, getch() will return different values, while getchar() will return 10 (ie \n)
1) The ENTER key on the windows platform will generate two escape characters \r\n, so getch Returns 13(\r).
2) The ENTER key in unix and linux systems only produces \n, so getch returns 10(\n).
3) The ENTER key in MAC OS will generate \r, so getch returns 13(\r).
getch(); is not a function in standard C and does not exist in the C language. So pay attention to the portability of the program when using it.

2,getchar();

The basic usage of getchar() and getch() is the same, the difference is that getch obtains the key value directly from the keyboard, does not wait for the user to press Enter, as long as the user presses a key, getch returns immediately, the
return value of getch is the ASCII code input by the user, and an error occurs Return -1. The input character will not be echoed on the screen.
When the user types a carriage return, getchar starts to read one character at a time from the stdio stream.
The return value of the getchar function is the first character entered by the user. ASCII code, if there is an error, return -1, and echo the characters entered by the user to the screen.
If the user enters more than one character before pressing Enter, other characters will be retained in the keyboard buffer area, waiting for subsequent getchar calls to read. That is to say, subsequent getchar calls will not wait for the user to press the key, but directly read the characters in the buffer, and wait for the user to press the key until the characters in the buffer are read. For example:
while ((c = getchar() ) != '\n')
printf(“%c”, c);

3,system(“pause”);

Common "press any key to continue"
headers: #include

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326398548&siteId=291194637