Linux under C Programming: GetChar () detailed

Source: Internet
Author: User

GetChar

Function Name: GetChar

Functions: Reading characters from stdin stream

Usage: int getchar (void);

Annotations:

GetChar has a return value of type int. When the program calls GetChar. The program waits for the user to press the key. The characters entered by the user are stored in the keyboard buffer. Until the user presses carriage return (the carriage return character is also placed in the buffer). When the user types in a carriage return, GetChar only begins to read one character at a time from the stdin stream. The return value of the GetChar function is the ASCII code of the first character entered by the user, such as error return-1, and the character entered by the user is echo to the screen. If a user enters more than one character before pressing ENTER, Other characters specifier remain in the keyboard cache, waiting for subsequent getchar calls to be read. That is, subsequent GetChar calls do not wait for the user to press the key, but to read the characters in the buffer directly until the characters in the buffer are read to wait for the user to press the key.

Getch and GetChar basic functions are the same, the difference is getch directly from the keyboard to get the key value, wait for the user to press ENTER, as long as the user presses a key, Getch immediately return, Getch return value is the user entered the ASCII code, Error return-1. The characters you enter do not echo back to the screen. Getch functions are often used in program debugging, when debugging, display the relevant results in a critical position for viewing, and then suspend the program with the Getch function, and the program continues to run when you press any key.

program Example:

#include <stdio.h>   
int main (void)   
{   
int c;   
/* Note: GetChar reads from stdin and are line buffered-this means it won't return until to press   
ENTE  R. *
/while ((c = GetChar ())!= ' \ n ')   
printf ("%c", c);   
return 0;   
}

Note: You can use the GetChar () function to wait for the programmer to press the keyboard to return to the editing interface after the program has finished debugging, use: At the end of the main function, returns 0; before adding GetChar ();

View a full set of articles: Http://www.bianceng.cn/Programming/C/201212/34807.htm

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.