The declaration of gets
is:
char * gets ( char * str );
Note the glaring omission of a maximum size for str.
cplusplus.com says2:
Notice that gets is quite different from fgets: not only gets uses stdin as source, but it does not include the ending newline character in the resulting string and does not allow to specify a maximum size for str (which can lead to buffer overflows).
And also:
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).
Now, of course, fgets
is commonly recommended as a replacement of gets
, because its declaration looks like this:
char * fgets ( char * str, int num, FILE * stream );
It DOES take a size parameter. This makes it much safer than gets
.
Now since I'm not willing to shell out money to download or buy the C11 standard
, can anyone shed some light on the reason for deprecating gets
and what it means for future code? Why did it exist in the same place when fgets
is safer? And why is it only just now being deprecated?