I don’t know if it is one of these days or what, but I can’t see what is going wrong here!
I was demonstrating the limitation of scanf() as it uses any white space to signal the end of the string being entered and as a result there is no way to enter multiword string using scanf(). in other words, you can’t enter “Hello, World!” using scanf() but you can using gets().
The following code, logically at least was supposed to run just fine. However, the sample run is weird. It doesn’t wait for me to enter string when it reaches gets(), instead it continues and print the second part of the first string I entered (if any) in the scanf() part of the program!!!
Now, if I comment out the first part of the program, the scanf() part, gets() works just fine. Any advice?
Walid Magd (MCP)
The primary challenge of every software development team is to engineer the illusion of simplicity in the face of essential complexity.
-Grady Booch
I was demonstrating the limitation of scanf() as it uses any white space to signal the end of the string being entered and as a result there is no way to enter multiword string using scanf(). in other words, you can’t enter “Hello, World!” using scanf() but you can using gets().
The following code, logically at least was supposed to run just fine. However, the sample run is weird. It doesn’t wait for me to enter string when it reaches gets(), instead it continues and print the second part of the first string I entered (if any) in the scanf() part of the program!!!
Now, if I comment out the first part of the program, the scanf() part, gets() works just fine. Any advice?
Code:
#include <stdio.h>
int main()
{
char Buffer[32];
char a[20];
printf("Please, Enter your name:\n");
scanf("%s",Buffer);
printf("Hello, %s\n",Buffer);
// Now, try it with gets()
printf("Please, Enter your name:\n");
gets(a);
puts("Hello, ");
puts(a);
}
Walid Magd (MCP)
The primary challenge of every software development team is to engineer the illusion of simplicity in the face of essential complexity.
-Grady Booch