From cplusplus.com
The most recent revision of the C standard (2011) has definitively removed this function from its specification
The function is deprecated in C++ (as of 2011 standard, which follows C99+TC3).
I just wanted to know what is the alternative to gets()
in C11 standard?
Use fgets() on the stdin stream. Note that unlike gets() , fgets() will not remove the newline character at the end of the input if it will fit in the buffer.
The gets() function reads characters from stdin and stores them in str until a newline character or end of file is found. The difference between gets() and fgets() is that gets() uses stdin stream. The gets() function provides no support to prevent buffer overflow if large input string are provided.
h/gets. gets is a function in the C standard library, declared in the header file stdio. h , that reads a line from the standard input and stores it in a buffer provided by the caller.
fgets() is a safer version of gets() where you can provide limitation on input size. You can also decide to take input from which stream(e.g. File or standard input).
In C11 gets
has been substituted by gets_s
that has the following declaration:
char *gets_s(char *str, rsize_t n);
This function will read at most n-1
chars from stdin
into *str
. This is to avoid the buffer overflow vulnerability inherent to gets
. The function fgets
is also an option. From http://en.cppreference.com/w/c/io/gets:
The gets() function does not perform bounds checking, therefore this function is extremely vulnerable to buffer-overflow attacks. It cannot be used safely (unless the program runs in an environment which restricts what can appear on stdin). For this reason, the function has been deprecated in the third corrigendum to the C99 standard and removed altogether in the C11 standard. fgets() and gets_s() are the recommended replacements.
Never use gets().
Given that gets_s
is defined in an extension to the standard, only optionally implemented, you should probably write your programs using fgets
instead. If you use fgets
on stdin
your program will also compile in earlier versions of C. But keep in mind the difference in the behavior: when gets_s
has read n-1
characters it keeps reading until a new line or end-of-file is reached, discarding the input. So, with gets_s
you are always reading an entire line, even if only a part of it can be returned in the input buffer.
Others have already answered the question. For the sake of completeness, this is the C standard's recommendation:
ISO9899:2011 K.3.5.4.1/6
Recommended practice
The fgets function allows properly-written programs to safely process input lines too long to store in the result array. In general this requires that callers of fgets pay attention to the presence or absence of a new-line character in the result array. Consider using fgets (along with any needed processing based on new-line characters) instead of gets_s.
So you should use fgets whenever possible.
EDIT
gets_s behavior is specified to be:
ISO9899:2011 K.3.5.4.1/4
Description
The gets_s function reads at most one less than the number of characters specified by n from the stream pointed to by stdin, into the array pointed to by s. No additional characters are read after a new-line character (which is discarded) or after end-of-file. The discarded new-line character does not count towards number of characters read. A null character is written immediately after the last character read into the array.
If end-of-file is encountered and no characters have been read into the array, or if a read error occurs during the operation, then s[0] is set to the null character, and the other elements of s take unspecified values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With