Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to supress warning "gets() is deprecated"? [duplicate]

everytime i try to input my string using gets() function, my compiler gives me warning like shown below. how to get rid of this. what am i doing wrong?

test.c:27:2: warning: ‘gets’ is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
gets(temp);
^
like image 548
Chintan Patel Avatar asked Oct 04 '14 12:10

Chintan Patel


People also ask

How do you solve for deprecated error?

Use fgets instead: fgets(temp, sizeof(temp), stdin); gets is deprecated because it's dangerous, it may cause buffer overflow.

What does gets is deprecated mean in C?

If you want to read a string from standard input, you can use the gets function, the name of which stands for "get string". However, this function is deprecated -- that means it is obsolete and it is strongly suggested you do not use it -- because it is dangerous.

What to use instead of gets in C?

To avoid Buffer Overflow, fgets() should be used instead of gets() as fgets() makes sure that not more than MAX_LIMIT characters are read.

Why was the gets () function removed from the library?

The gets() function provides no support to prevent buffer overflow if large input string are provided. It is defined in <cstdio> header file. Note: Avoid using the gets() function as it can be dangerous for the program. This function was deprecated in C++11 and removed from C++14.


1 Answers

Use fgets instead:

fgets(temp, sizeof(temp), stdin);

gets is deprecated because it's dangerous, it may cause buffer overflow.

like image 168
Yu Hao Avatar answered Oct 27 '22 11:10

Yu Hao