how to supress warning “gets() is deprecated”? [du

2019-02-14 06:28发布

问题:

This question already has an answer here:

  • Disable warning messages in GCC through header files? 10 answers

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);
^

回答1:

Use fgets instead:

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

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