While using gets()
in my code, the compiler shouts
warning: the 'gets' function is dangerous and should not be used.`
and
warning: ‘gets’ is deprecated (declared at /usr/include/stdio.h:638)
[-Wdeprecated-declarations]
Any specific reasons?
gets
may cause buffer overflow, since it don't consider length of the data. More details are here : gets() function in C
deprecated message means, this function is marked as deprecated and may remove from standard in later time. So discouraging user to use it.
Yes, because, the
gets()
function is dangerous, as it suffers from buffer overflow issue. Anyone should refrain from using that.Also, regarding the warning with
-Wdeprecated-declarations
,gets()
is no longer a part ofC
standard [C11
onwards]. So, C librariescompilersare not bound to support that any more. It can be removed in future. To warn the developer about the potential pitfall and to discourage the further usage ofgets()
, the compiler## emits the warning message.(##) To be pedantic, the warning is not generated by the compiler (
gcc
) all by itself, rather caused by apragma
or attribute on the implementation ofgets()
in theglibc
that causes the compiler to emit the warning. [Courtesy, FUZxxl, from the dupe answer.]