In C, there are at least two functions that can be refactored to obtain a string:
1.SCANF ()
The header file: stdio.h
Syntax: scanf ("Format control string", variable address list);
When accepting a string: scanf ("%s", character array name or pointer);
2.gets ()
The header file: stdio.h
Syntax: Gets (character array name or pointer);
Both when the string is accepted:
1. Different points:
SCANF can not accept spaces, Tab tab, carriage return, etc.;
And gets can accept space, Tab tab and carriage return, 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);
}
In turn, type ASD space FG carriage return, ASD space FG return, 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 carriage return, then ch1= "Asdfg\0", c1= ' \ n ', ch2= "Asdfg\0", the C2 needs to enter.