Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why gets() is deprecated? [duplicate]

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?

like image 689
network-programmer Avatar asked Nov 05 '25 22:11

network-programmer


1 Answers

Can someone explains why the compiler shows like that…?

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 of C standard [C11 onwards]. So, C libraries compilers are 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 of gets(), the compiler## emits the warning message.


(##) To be pedantic, the warning is not generated by the compiler (gcc) all by itself, rather caused by a pragma or attribute on the implementation of gets() in the glibc that causes the compiler to emit the warning. [Courtesy, FUZxxl, from the dupe answer.]

like image 108
Sourav Ghosh Avatar answered Nov 07 '25 14:11

Sourav Ghosh