The key is that the scanf return value is Integer, while (scanf ()! =eof)
And the gets return is the pointer while (gets ()! =null)
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.
Both the scanf () function and the Get () function are available for input strings, but are functionally different. If you want to enter the string "Hi Hello" from the keyboard, you should use the __GETS__ function.
The get can receive spaces, and scanf encounters a space, carriage return, and TAB key will assume that the input ends, and that it cannot receive spaces.
Char string[15]; Gets (string); /* Encountered a carriage return that the input ended */
scanf ("%s", string); /* Encountered a space thought input ended */
So when you include spaces in the string you enter, you should use the gets input.
scanf and gets the difference when getting a string
In the C language, there are at least two functions that can get a string:
1.SCANF ()
Header file: stdio.h
Syntax: scanf ("Format control string", variable address list);
When string is accepted: scanf ("%s", character array name or pointer);
2.gets ()
Header file: stdio.h
Syntax: Gets (character array name or pointer);
Both when accepting a string:
1. Different points:
SCANF cannot accept space, Tab tab, carriage return, etc.;
And get can accept space, Tab tab and enter, etc.;
2. The same point:
The string is automatically added after the end of the acceptance.
Example 1:
#include <stdio.h>
Main ()
{
Char ch1[10],ch2[10];
scanf ("%s", CH1);
Gets (CH2);
}
Type ASD space FG carriage return, ASD space FG Enter, then ch1= "asd\0", ch2= "ASD fg\0".
Example 2:
#include <stdio.h>
Main ()
{
Char ch1[10],ch2[10],c1,c2;
scanf ("%s", CH1);
C1=getchar ();
Gets (CH2);
C2=getchar ();
}
Type ASDFG carriage return, ASDFG Enter, then ch1= "Asdfg\0", c1= ' \ n ', ch2= "Asdfg\0", C2 needs to be entered.
SCANF: When a carriage return is encountered, the Space and tab keys are automatically added after the string, but the carriage return, the Space and tab keys remain in the input buffer.
Get: Can accept all the characters entered before the Enter key, and use ' \ n ' instead of '/'. Enter will not be left in the input buffer
gets () used to read the string, end the input with a carriage return
SCANF () can read all types of variables
(reproduced from: http://leoenglish.blog.163.com/blog/static/1750319852011296336486/) Thanks
scanf and gets read the string