The difference between gets and getchar: the gets function inputs a line of string, ending with a carriage return, and the carriage return key will be filtered out and will not be read into the string, while the getchar function reads a character , including the Enter key, will also be read as one character.
Function prototype:
char * gets(char * ptr);
int getchar(void);
Function usage:
1. gets() is used to read from standard input The stream stdin reads an entire line (ending with '\n' or EOF), writes the character array pointed to by ptr, and returns this pointer; NULL is returned when an error occurs or the end of file is encountered. The '\n' at the end of the line is taken from the stream but not written to the array. gets() does not check the size of the array being written.
2. getchar() is used to read a character from the standard input stream stdin and return this character. If the end of file is reached, EOF is returned. Note that EOF cannot be represented by the char type, so the getchar() function returns an int type number. You should also pay attention to this when using it.
Recommended tutorial: "C#"
The above is the detailed content of What is the difference between gets and getchar in C language?. For more information, please follow other related articles on the PHP Chinese website!