pls can you explain why example1 is not working and example2 works? Or explain how can I put character with whitespace to string used in struct? I am beginner at programming C, in other problems I was able to find solution but in this case I do not really know :(
example1 (there not working whitespace string):
#include <stdio.h>
struct string {
char word[20];
char sentence[100];
} s;
int main() {
printf("Enter word:\t");
scanf("%s", s.word); //1st input without whitespace
printf("\nEnter more words with whitespace:\t");
scanf("%[^\n]s", s.sentence); //2nd input with whitespace - not working
printf("\nWord is: %s", s.word); // 1st input printed (OK)
printf("\nSentence is: %s \n", s.sentence); // 2nd input not printed (NOT! OK)
return 0;
}
example2 (working whitespace string):
#include <stdio.h>
struct string {
char word[20];
char sentence[100];
} s;
int main() {
printf("\nEnter more words with whitespace:\t");
scanf("%[^\n]s", s.sentence); //1st input with whitespace
printf("Enter word:\t");
scanf("%s", s.word); //2nd input without whitespace
printf("\nWord is: %s", s.word); // 2nd input printed (OK)
printf("\nSentence is: %s \n", s.sentence); // 1st input printed (OK)
return 0;
}
Between "example1" and "example2" are different order of input (see at code comment). I dont know how can I use whitespace to scanf input in struct. And sorry for my english.
Thank you in advance!