Home > Article > Backend Development > What are the input functions in C language?
The input functions of the c language are: 1. scanf() function, which reads formatted input from the standard input stdin; 2. getchar() function, which obtains a character from the standard input stdin; 3. gets() The function reads a line from the standard input stdin; 4. The getch() function reads a string from the stdin stream and stops when a newline character or EOF is received.
The operating environment of this tutorial: Windows 7 system, c99 version, Dell G3 computer.
The input functions of c language are:
1. The return value of scanf
scanf() function returns a successful assignment The number of data items. If an error occurs when reading the end of the file, EOF will be returned.
For example:
scanf("%d%d", &a, &b);
If both a and b are successfully read, then the return value of scanf is 2
If only a is successfully read, the return value is 1
If neither a nor b is successfully read, the return value is 0
If an error is encountered or end of file is encountered, the return value is EOF
and returns The value is int type
Blank character: A blank character will cause the scanf() function to omit one or more blank characters in the input during the read operation.
Non-whitespace character: A non-whitespace character will cause the scanf() function to eliminate characters that are the same as the non-whitespace character when reading.
Things you should pay attention to when using the scanf function
(1) For string arrays or string pointer variables, since the array name and pointer variable name themselves are addresses, when using the scanf() function , there is no need to add the "&" operator in front of them.
(2) An integer can be added between each format specifier of "%" in the format string to represent the maximum number of digits in any read operation.
(3) There is no precision control in the scanf() function.
For example: scanf("%5.2f",&a); is illegal. You cannot attempt to use this statement to enter a real number with 2 decimal places.
(4) Scanf requires a variable address. If a variable name is given, an error will occur.
For example, scanf("%d",a); is illegal and should be changed to scnaf ("%d",&a); is legal.
(5) When inputting multiple numerical data, if there are no non-format characters in the format control string to separate the input data, spaces, TAB or carriage returns can be used as intervals.
C compiler will consider the data to end when it encounters a space, TAB, carriage return or illegal data (for example, when "12A" is entered for "%d", A is illegal data).
(6) When inputting character data (%c), if there are no non-format characters in the format control string, all entered characters are considered to be valid characters.
(7) If there are non-format characters in the format control string, the non-format characters must also be entered when inputting.
2. getchar
getchar has an int return value. When the program calls getchar, the program waits for the user to press the key. The characters entered by the user are stored in In the keyboard buffer. Until the user presses Enter (the carriage return character is also placed in the buffer). When the user types Enter, getchar 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. If an error occurs, -1 is returned, and the characters entered by the user are echoed to the screen. If the user enters more than one character before pressing Enter, the other characters will be retained in the keyboard buffer area. , waiting for subsequent getchar calls to read. In other words, subsequent getchar calls will not wait for the user to press the key, but directly read the characters in the buffer. They will not wait for the user to press the key until the characters in the buffer have been read.
3. getch
The basic functions of getch and getchar are the same. The difference is that getch obtains the key value directly from the keyboard and does not wait for the user to press Enter. As long as the user presses a key, getch It returns immediately. The return value of getch is the ASCII code entered by the user. If an error occurs, -1 is returned. The entered characters will not be echoed on the screen. The getch function is often used in program debugging. During debugging, relevant results are displayed in key locations. To be viewed, then use the getch function to pause the program. When any key is pressed, the program continues to run.
4, gets
Function: Read a string from the stdin stream , until it stops when a newline character or EOF is received, and stores the read result in the character array pointed to by the buffer pointer. The newline character is not used as the content of the read string. The read newline character is converted into a null value and ends the string.
Return value: If the reading is successful, the same pointer as the parameter buffer is returned; if EOF (End-of-File) or an error occurs during the reading process, a NULL pointer is returned. Therefore, when encountering a return value of NULL, use the ferror or feof function to check whether an error occurred or EOF was encountered.
If you want to know more about programming learning, please pay attention to the php training column!
The above is the detailed content of What are the input functions in C language?. For more information, please follow other related articles on the PHP Chinese website!