Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
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():
int n;
char str[80];
printf("enter a number: ");
scanf("%d", &n);
printf("enter a string: ");
gets(str);
printf("you typed %d and "%s"\n", n, str);
but the compiler seems to be skipping the call to gets()!
✍: Guest
If, in response to the above program, you type the two lines
42
a string
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, 1097👍, 0💬
Popular Posts:
Should synchronization primitives be used on overrided bean methods? No. The EJB specification speci...
What is a measure in OLAP ? Measures are the key performance indicator that you want to evaluate. To...
How do you locate the first X in a string txt? A) txt.find('X'); B) txt.locate('X'); C) txt.indexOf(...
How can I use tables to structure forms Small forms are sometimes placed within a TD element within ...
What’ is the sequence in which ASP.NET events are processed ? Following is the sequence in which the...