Mail Archives: djgpp/2000/03/23/09:52:29
David Cleaver <davidis AT ou DOT edu> wrote:
> Ok, I just looked up the info and found out that scanf() only reads
> up to the first whitespace character. So, the function you want to
> use is gets() which will get everything up to, but not including,
> the new-line character. You can use it thus:
Actually, it's a well-known in rule in the C language newsgroups that
you do *not* want to use gets() for anything, ever. Some systems' C
libraries will even output a warning message everytime a programs uses
gets().
> char name[41];
> gets(name);
> /*
> The gets() function will read 40 characters from the input stream
> and put them in the variable _name_.
*No*. gets() will read as many characters the user types in, before
the line end. If anyone ever types more than 40, h*ll will break
loose.
For all your programs, whenever you think you want to use gets(), use
fgets() instead, which does allow you to restrict the amount of input
it'll handle.
For loosely related reasons, you almost never want to use scanf(),
either. Use fgets() and sscanf() instead.
--
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.
- Raw text -