I am reading a number with scanf and d, and then a string with gets
I'm reading a number with scanf and %d, and then a string with gets():
printf("enter a number: ");
printf("enter a string: ");
printf("you typed %d and "%s"\n", n, str);
but the compiler seems to be skipping the call to gets()!
If, in response to the above program, you type the two lines
scanf will read the 42, but not the newline following it. That newline will remain on the input stream, where it will immediately satisfy gets() (which will therefore seem to read a blank line). The second line, ``a string'', will not be read at all.
If you had happened to type both the number and the string on the same line:
42 a string
the code would have worked more or less as you expected.
As a general rule, you shouldn't try to interlace calls to scanf with calls to gets() (or any other input routines); scanf's peculiar treatment of newlines almost always leads to trouble. Either use scanf to read everything or nothing.
2015-10-23, 959👍, 0💬
If we inherit a class do the private variables also get inherited ? Yes, the variables are inherited...
Can include files be nested? The answer is yes. Include files can be nested any number of times. As ...
Why is it preferred to not use finalize for clean up? Problem with finalize is that garbage collecti...
.NET INTERVIEW QUESTIONS - Is versioning applicable to private assemblies? Versioning concept is onl...
What Is Posting? Posting is an event that writes Inserts, Updates and Deletes in the forms to the da...