For a very simple scanf function, has been used, but there are a lot of knowledge points are not mastered well, is summarized as follows: 1, a number of scanf after the order to scanf ("%c", &c)
When the program calls the SCANF function in succession, the previous receive input receives, the general end is a white space character (space, enter), such as Enter end input, but when there is another with scanf ("%c", &a), then the above output Enter as the Terminator, which is entered into a, causing a to be empty. (1) When you continue typing with%d:
<PRE>intMainvoid){ intA, b, C =0; printf ("input a,b\n"); scanf ("%d,%d", &a,&b); printf ("input c\n"); scanf ("%d", &c); printf ("A =%d, B =%d, c =%d\n", A, b, c); while(1);}input A, b1,2input C3a=1, B =2, C =3</PRE>
(2) When you continue to enter with%c:
int Main (void) { int0; scanf ("%d,%d", &a,&b); scanf ("%c", &c); printf ("a =%d, B =%d, c =%c\n", A, b, c); while (1);}
Results:
input A, b 1,212, C =
It turned out to be amazing, and when I entered the end of the input: Enter, the result was printed directly, and C was printed without a manual input. Explanation of Reason:
Enter a value of A/b when entered: Enter. So the input data has three data, namely: 1 2 Enter, three data. When all are in%d format as input, 1 assignment to a,2 to B, but enter is obviously not in%c format, it is passed off, only when the input is a number, and the input value 3 is assigned to C.
When it is:%d,%d,%c continuous input, we know that the input is the enter,1 assignment a,2 assignment to B, and at this time,%c, the requirement is to enter a character, will be assigned to C, so this causes the C print result is empty. How the scanf works:
SCANF works by saving any input from the user into the buffer, and when there is a scanf, reads the data from the queue of the buffer until it encounters a blank character (space, enter, tab, page break) or not at the time of the specified type, and ends the input. Therefore, when the continuous input is%d,%f,%x and other data formats, you can automatically skip whitespace characters, because the type does not match, until the number of digits;
Solution:(1) By judging whether the input is enter
intMainintargcChar*argv[]) { intA, b, C =0; scanf ("%d,%d", &a, &b); Do{scanf ("%c", &AC); } while(c = ='\ n'); printf ("A =%d, B =%d, c =%c\n", A, b, c); while(1);}
A Do While loop is added to get the input to be true, that is, the input is entered as enter or other whitespace characters, then the loop input, when the input is the required data type (%c), exits the loop body, guaranteed to obtain the C value. (2) Enter add white space character (space)
<PRE>intMainintargcChar*argv[]) { intA, b, C =0; scanf ("%d,%d", &a, &b); scanf ("%c", &c); A space is added in front of%c, which is preceded by a space allowed (enter) printf ("A =%d, B =%d, c =%c\n", A, b, c); while(1);}</PRE>
Actually is scanf ("%c", &c), added a space more. Space%c: Specifies that the input content is: a space +%c mode. 2. Input of multiple characters consecutively
When you need to enter multiple characters consecutively:
<PRE>int main (intChar *argv[]) { char A, b; scanf ("%c", &a); scanf ("%c",&b); printf ("a =%c, B =%c\n", A, b); while (1);} </PRE>
The result is:
AA = A, B =
will also assign the Enter value to B, Solution: (1) Add a space
<PRE>int main (intChar *argv[]) { char A, b; scanf ("%c", &a); scanf (" %c",&b); printf ("a =%c, B =%c\n", A, b); while (1);}
Result input:
ABA = A, B = b
Reasons do not explain, the above has been well explained. (2) Add do while to judge
<PRE>intMainintargcChar*argv[]) { CharA, B; scanf ("%c", &a); Do{scanf ("%c", &b); } while(b = ='\ n'); printf ("a =%c, B =%c\n", A, b); while(1);}
Summary of issues in successive calls to scanf