look at the code given below
#include <stdio.h>
int main()
{
char str[20];
printf("Enter a string : ");
scanf("%s", str);
printf("String : %s", str);
return 0;
}
output will be,
Enter a string : Hello world
String : Hello
my question is why it take only first word as input?
CodePudding user response:
my question is why it take only first word as input?
Because that's exactly what the documentation says that the %s
specifier should do:
Any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character is automatically added at the end of the stored sequence.
https://www.cplusplus.com/reference/cstdio/scanf/
CodePudding user response:
You can use the following methods
Method 1:
scanf("%[^\n]",str);
Method 2:
fgets(str, MAX_LIMIT, stdin);