Disable warning: the `gets' function is dangerous in GCC through header files?

Biswajyoti Das picture Biswajyoti Das · Jul 31, 2009 · Viewed 16.2k times · Source

I am using the function gets() in my C code. My code is working fine but I am getting a warning message

(.text+0xe6): warning: the `gets' function is dangerous and should not be used.

I want this warning message not to pop up. Is there any way?

I am wondering that there might be such possibilities by creating a header file for disabling some warnings. Or is there any option during compiling that can serve my purpose? Or may be there is a particular way of using gets() for this warning not to pop up?

Answer

anon picture anon · Jul 31, 2009

The obvious answer is to learn from what the compiler is trying to tell you - you should never, ever, use gets(), as it is totally unsafe. Use fgets() instead, which allows you to prevent possible buffer overruns.

#define BUFFER_SIZE 100
char buff[BUFFER_SIZE];
gets( buff);   // unsafe!
fgets( buff, sizeof(buff), stdin );   // safe