Never use the function gets
. Why? It is unsafe. Since the maximum string size cannot be specified, it is always susceptible to buffer overflow.
This snippet shows one way to do a safe version of gets
. It reads characters from the stdin
into a string up to the specified size and discards the trailing newline. It does not remove excess characters from the stdin
either.
See also Read a Line of Text from the User.