Getch (), Getche (), and GetChar () functions

Source: Internet
Author: User

Original: Getch (), Getche (), and GetChar () functions

Getch (), Getche (), and GetChar () functions

(1) Getch () and Getche () functions
Both of these functions read a character from the keyboard. Its invocation format is:
Getch ();
Getche ();
The difference is that the getch () function does not echo the read-in character on the display screen, while the Getche () function echo the read-in Word to the display screen

On

Example 1:

1#include"stdio.h"
2#include"conio.h"
3 voidMain ()
4{
5 Charc, ch;
6C=getch ();/*read a character from the keyboard without echoing to the character variable C*/
7Putchar (c);/*Output this character*/
8Ch=getche ();/*reads a character from the keyboard to the character variable ch*/
9Putchar (CH);
Ten}

With Echo and non-ECHO features, these two functions are often used to perform functions such as pausing during interactive input.
Example 2:

1#include"stdio.h"
2#include"conio.h"
3 voidMain ()
4{
5 Chars[ -];
6printf"Name:");
7Gets (s);
8printf"Press any key to continue ...");
9Getch ();/*wait for any key to enter*/
Ten}

(2) GetChar () function
The GetChar () function also reads a character from the keyboard and brings back the display. It differs from the previous two functions in that: the GetChar () function waits for input straight

Until you press ENTER to end, all input characters before carriage return are displayed on the screen one by one. But only the first character is the return value of the function.

Example 3:

    1. #include "stdio.h"
    2. #include "conio.h"
    3. void Main ()
    4. {
    5. char c;
    6. C=getchar (); /* Read characters from keyboard until end of carriage return */
    7. Putchar (c); /* Displays the first character of the input */
    8. Getch (); /* wait to press Ning */
    9. }

Example 4:

    1. #include "stdio.h"
    2.        #include "conio.h"
    3.        void Main ()
    4.       {
    5.            char c;
    6.            while ((C=getchar ()) = ' \ n ')    /* per GetChar () Read in one character */
    7.            printf ("%c", c);     /* Output AS-is */
    8.            getch ();        / * Wait for any health */
    9.       }

Programming, in case the program runs a flash is lost, always in the end to a "getch ();"
Getch () and Getche () require "conio.h"
Report:
Getch read one character from the keyboard without echoing back
Call format #include "stdio.h"
int getch (void);
Prototype in Conio.h
Description Getch reads a character directly from the keyboard and does not echo back to the screen.
The return value Getch returns the characters that are read from the keyboard.
Portability Getch only available for DOS

GetChar to take characters from the stdin stream
Call format #include "conio.h"
int GetChar (void);
Prototype in Stdio.h
Description GetChar is a macro that returns the next character of the specified stdin input stream, which is defined as GETC (stdin).
When the return value call succeeds, GetChar returns the read character, which has been converted to an integer value for the unsigned extension. When you encounter a file end or an error,

Returns EOF.
Portability GetChar for UNIX systems, defined in ANSI C, compatible with Kernighan and Ritchie definitions

Getch (), Getche (), and GetChar () functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.