1. The scanf function has a return value, and its return value can be divided into three different cases
1) A positive integer that indicates the number of correct input parameters. For example, execute scanf ("%d%d", &a, &b);
If the user input "3 4", you can enter correctly, return 2 (correctly entered two variables);
If the user enters "3,4", you can enter a correctly, you cannot enter B, and return 1 (a variable is entered correctly).
2) 0, indicating that the user's input does not match and that no value can be entered correctly. As the example above, if the user enters ", 3 4", returns 0.
3) EOF, which is a constant defined within the stdio.h (usually a value of-1), indicating that the input stream has ended. Under Windows, the user presses CTRL + Z (sees a ^z character) and then presses ENTER (which may need to be repeated 2 times), which means that the input ends; Linux/unix uses ctrl+d to indicate the end of the input.
So you can use the following code to handle the input:
While (scanf ("%s%c%c", str, &oldchar, &newchar) = = 3)/* or! = EOF , but the former is better */
{
; Processing
}
2. Why do I need spaces between%s and%c in the scanf format string above?
That's because if there's no space ... The Oldchar input is a space = =.
3. By the way, the return value of printf is the number of characters in the output, for example, the return value of printf ("1234") is 4, and the return value of printf ("1234\n") is 5.
The return value of the C language sacanf