I just tried to print the String which I have received through scanf function and I have set the loop to process further until I press the 0 to terminate.
First I have entered the single string, it was normally printed, but another time I have entered three strings => Name1 Name2 Name3 --> three strings were printed automatically like I mentioned below after the code
Program:
#include <stdio.h>
int main()
{
int i;
do {
char str[20];
printf("\nEnter the String: ");
scanf("%s", str); // String input
printf("The String is %s", str); // Print the String
printf("\n\nEnter the Number 1 to restart and 0 to terminate: "); //Input for loop
scanf("%d", &i);
} while (i != 0); // while loop
return 0;
}
Output console Screen:
Enter the String: Name
The String is Name
Enter the Number 1 to restart and 0 to terminate: 1 // Normally printed
// I don't what is happening by Entering three strings with space (Below Mentioned)
Enter the String: Name1 Name2 Name3
The String is Name1
Enter the Number 1 to restart and 0 to terminate:
Enter the String:
The String is Name2
Enter the Number 1 to restart and 0 to terminate:
Enter the String:
The String is Name3
Enter the Number 1 to restart and 0 to terminate: 0
...Program finished with exit code 0 Press ENTER to exit console.
If you want to enter a string that contains several words separated by spaces then instead of this call of
scanfwrite
Pay attention to the leading space in the format string. It allows to skip the new line character
'\n'stored in the input buffer due to pressing the Enter key. Otherwise neither string can be read.As for your code then due to the input buffer contains three entered strings then this call
of inputting of an integer fails between reading the strings because the input buffer contains letters instead of an integer.
After all three strings will be read then only then the call of scanf will, wait until you will enter a number.