我尝试通过scanf读取两个字符串,但只能读取第一个字符串。第二个scanf无法读取字符串,一旦控制进入第二个printf,就会获得一些垃圾值。
code:
#include<stdio.h>
#include<string.h>
int main(){
char string1[20], string2[20];
printf("Enter the first string :");
scanf("%[^\n]s", string1);
printf("Enter the second string:");
scanf("%[^\n]s", string2);
printf("First string = %s\n", string1);
printf("Second string = %s\n", string2);
return 0;
}
output:
$ ./a.out
Enter the first string :c programming
Enter the second string:First string = c programming
Second string = �@
$