Differences for getCH (), getche (), getchar () (ZZ)

xiaoxiao2021-03-05  26

This problem, Tan Haoqiang's C language design has not been clear, or not.

Check out other information, finally made it clear.

GetCh (), getche () and getchar () functions

(1) getCH () and getche () functions

Both functions are read from the keyboard into a character. Its call format is:

Getch ();

GetChe ();

The difference between the two is: getch () function does not display the read character back on the display screen, and getChe ()

The function returns the read character back to the display screen.

example 1:

#include

Main ()

{

CHAR C, CH;

C = getch (); / * Reads a character from the keyboard does not return to the character variable C * /

PUTCHAR (C); / * Output this character * /

CH = getChe (); / * Read a character from the keyboard to send a character to the character variable CH * /

PUTCHAR (CH);

}

Using the characteristics of echo and non-return, these two functions are often used in the process of interacting input.

Waiting for functions.

Example 2:

#include

Main ()

{

CHAR C, S [20];

Printf ("Name:");

Gets (s);

Printf ("Press Any Key To Continue ...");

getCh (); / * Wait for entering any key * /

}

(2) GetChar () function

The getChar () function is also read from the keyboard and takes back. It with the previous two functions

The difference is that the getchar () function is waiting to enter until the carriage is over, and all the input words before entering the bus.

The characters will be displayed on the screen one by one. But only the first character is the return value of the function.

The Call format of the getchar () function is:

GetChar ();

Example 3:

#include

Main ()

{

Char C;

c = getchar (); / * Read the character from the keyboard until the end of the carriage return * /

Putchar (c); / * Display the first character * /

Getch (); / * Wait for a health * /

}

Example 4

#include

Main ()

{

Char C;

While (c = getchar ())! = '/ n') / * Each getchar () reads a character * /

Printf ("% c", c); / * Output * /

Getch (); / * Wait for a health * /

}

转载请注明原文地址:https://www.9cbs.com/read-35168.html

New Post(0)