Because scanf interprets space-separated strings as different input arguments. This short and horrible program demonstrates what I'm talking about:
If you type "abcdefg 123456 ABCDEFG" when the program runs, printf will put "abcdefg" in myarray, "123456" into myarray2, and "ABCDEFG" into myarray3.
Code:
#include <stdio.h>
int main(void)
{
char myarray[100];
char myarray2[100];
char myarray3[100];
printf("%s\n%s\n%s\n%d\n", myarray, myarray2, myarray3, scanf("%s%s%s", myarray, myarray2, myarray3));
return 0;
}
The %d, by the way, illustrates that scanf's return value is the number of strings successfully read. So in this case, if you typed out what I said when the program ran, you'd see something like
Code:
abcdefg
123456
ABCDEFG
3
as the output.
Code:
scanf("%s%s%s", myarray, myarray2, myarray3);
is the same as
Code:
scanf("%s", myarray);
scanf("%s", myarray2);
scanf("%s", myarray3);
so a more readable version of the program would be this:
Code:
#include <stdio.h>
int main(void)
{
int i = 0;
char myarray[100];
char myarray2[100];
char myarray3[100];
i+= scanf("%s", myarray);
i+= scanf("%s", myarray2);
i+= scanf("%s", myarray3);
printf("%s\n", myarray);
printf("%s\n", myarray2);
printf("%s\n", myarray3);
printf("%d\n", i);
return 0;
}
(Well, there is one difference: the second you have to press Enter after "abcdefg", "123456", and "ABCDEFG", but with the first you just separate them with spaces...)