This function is declared in the stdio.h header file, and should be used with the stdio.h header file. For example:
# include & ltstdio.h & gt
int getchar(void);
(2) syntax: int getchar (void);
(3) Return value: the return value of the getchar function is the ASCII code of the first character input by the user. If there is an error, return-1, and the characters entered by the user will be echoed to the screen. If the user enters more than one character before pressing Enter, the other characters will be saved in the keyboard buffer, waiting for the subsequent getchar call 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.
(4) Function: Read only one character (including spaces, carriage returns and tabs) from the standard input stream, exit when reading carriage returns ('\n'), and all characters entered by the keyboard are stored in the buffer.
Type enter getchar to enter the buffer to read characters, and only the first character is returned as the value of the getchar function at a time. If there are loops or enough getchar statements, all characters in the buffer will be read out in turn until' \n'.
To understand this, the reason why you read a series of characters in turn is because the loop makes it possible to repeatedly use getchar to read the characters in the buffer instead of reading multiple characters. In fact, getchar can only read one character at a time.
If you need to cancel the influence of' \n', you can use getchar () to clear it, such as while((c=getchar ())! ='\n'), where getchar (); I just got' \n' but didn't assign it to any character variable, so it won't affect it, which is equivalent to clearing this character.
Extended data:
Program example:
# include & ltstdio.h & gt
# include & ltconio.h & gt
Major (invalid)
{
int c;
int a;
a = getchar();
if (EOF! =a)
printf("%c ",a);
while((c=getchar())! ='\n')//c The received value is carriage return' \n'. Enter the first character and press \ n'. C will not be displayed.
{
if (EOF==a)
Break;
printf("%d ",c);
}
getchar();
}
/* getchar()- Note that getchar is read from stdin and is line-buffered; ?
This means it won't return until you press enter. */
Note: You can use the getchar () function to let the programmer press the keyboard after the program debugging and before returning to the editing interface. Usage: The main function ends and returns 0; ; Add getchar () before it; When you don't use enter before getchar (), you can use this function to let the programmer press the keyboard to return to the editing interface after the program debugging, otherwise you can return to the editing interface directly. For example:
# include & ltstdio.h & gt
int main(void)
{
int I;
scanf("%d ",& ampI);
getchar();
/* When using this function here, press Enter after entering the function, and the operation of the function will be directly completed at lightning speed */
}
Note: Of course, you can also use the header file stdlib.h, and then add system("pause ") before return 0, which can also produce the same effect (Windows only).
References:
Baidu Encyclopedia -getchar ()
Baidu encyclopedia -getchar (computer language function)