What is the difference between scanf and getchar? What is the use of getchar? scanfgetchar
Let's look at the getchar () function again today and find that it is confused with the knowledge of the scanf function. Find the information below.
Character data input:
Scanf () functionIs the format input function, that is, input data from the keyboard to the specified variable in the format specified by the user.
In the format string of the scanf () statement, because no non-format character is used as the input interval between "% d ",Use more than one space or carriage return character as the interval between each two inputs..
Scanf () skips spaces, tabs, and line breaks when reading numbers!
See the following program.
1 #include<stdio.h>2 int main()3 {4 int a,b,c;5 scanf("%d%d%d",&a,&b,&c);6 printf("%d%d%d",a,b,c);7 }
Getchar () functionIs a keyboard input function, which is used to input a character from the keyboard. Simply put, the function of the getchar () function in the C program isReceives one character.
First, let's look at the scanf () program below:
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main(void) 4 { 5 int a,b,c; 6 printf("please input num a:\n"); 7 scanf("%d",&a); 8 printf("please input num b:\n"); 9 scanf("%d",&b);10 printf("please input num c:\n");11 scanf("%d",&c);12 printf("%d,%d,%d",a,b,c);13 }
Running result:
Let's look at the following char program:
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main(void) 4 { 5 char a,b,c; 6 printf("please input num a:\n"); 7 scanf("%c",&a); 8 printf("please input num b:\n"); 9 scanf("%c",&b);10 printf("please input num c:\n");11 scanf("%c",&c);12 printf("%c,%c,%c",a,b,c);13 }
Running result:
Why? Why can't I enter num B? ?
Let's take a look at the source code:
The program receives three consecutive characters. In C, the enter key represents a line break. Therefore, after entering the value 'q' of a, press enter. The line break is immediately received by character B, then the program goes down and enters the value 'W' of c '. The values of a, B, and c are displayed in order. We know that a = 'Q', B = '\ n', c = 'W '; therefore, two rows are displayed.
Finally, let's look at the getchar () program below:
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main(void) 4 { 5 char a,b,c; 6 printf("please input num a:\n"); 7 scanf("%c",&a); 8 getchar(); 9 printf("please input num b:\n");10 scanf("%c",&b);11 getchar();12 printf("please input num c:\n");13 scanf("%c",&c);14 printf("%c,%c,%c",a,b,c);15 }
Running result:
Finally, let's look at the following program:
1 #include <stdio.h>2 int main()3 {4 char a,b;5 a=getchar();6 b=getchar();7 putchar(a);8 putchar(b);9 }
Enter x-press ENTER-y-press Enter.
Will the result be xy? No result is a = x B = '\ n' (Press ENTER)
When we start to play x, x is in the buffer. When we press enter, the first getchar () the value of x is successfully saved to a, but the carriage return is both a confirmation and a character. The carriage return '\ n' is followed by the buffer and is released along with x, x to the program, press enter ('\ n') to the program and get it by 2nd getchar (). At this time, we enter y. Then there is a character 'y' in the buffer zone ', then press enter, and 'y' and '\ n' are prepared to be released again. Because there are no 3rd getchar () characters in the program (), so 'y' and '\ n' remain in the buffer, but they are already in the preparation status. If the program returns a getchar (), 'y' you do not need to press Enter. It will directly enter 3rd getchar (), getchar () is obtained in sequence, in order, won't get '\ n' first ', '\ n' is waiting to enter 4th getchar () immediately ()