As a beginner to code I'm following a free course on C, and currently trying to understand strings.
To test things out I've been writing the following :
#include <stdio.h>
#include <stdlib.h>
int main()
{
char test[] = "What's your name ?\n\n";
char firstName[100];
char lastName[100];
printf("%s", test);
printf("What's your first name ?\n");
scanf("%s", firstName);
printf("What's your last name ?\n");
scanf("%s", lastName);
printf("Your name is %s %s", firstName, lastName);
return 0;
}
When I run it, the first printf() displays test[] normally.
The rest of the program works correctly ONLY IF there are no spaces in firstName or lastName.
If there's a space in firstName, the program will skip the second scanf and display firstName. If there are two spaces in firstName, the program will skip the second scanf and display only the first two words in firstName. If there's a space in lastName the program will display firstName and only the first word in lastName.
I don't understand why it does so, and especially why the second scanf gets skipped when there's a space in firstName. The first printf shows that spaces should be handled without issues so why does it malfunction when I use them with scanf ?
Thank you for your attention.