0

although i wrote #pragma warning(disable:4996) in the top of my code(in visual studio 2019), but compile says that "gets" is undefined. How can i fix it ???

#pragma warning(disable:4996) 
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    char str[10];
    gets(str);
    printf("%s", str);

}
tadman
  • 208,517
  • 23
  • 234
  • 262
Khánh Phạm
  • 31
  • 1
  • 7
  • Use `fgets(str, sizeof str, stdin)` instead. `gets` has been removed from the language. – user3386109 Apr 21 '21 at 04:29
  • 1
    This is a feature, not a bug. Also don't use such ridiculously tiny buffers. Consider 1024 as a more reasonable default. – tadman Apr 21 '21 at 04:29
  • 1
    [MS gets manual](https://learn.microsoft.com/en-us/cpp/c-runtime-library/gets-getws?view=msvc-160): *These functions are obsolete. Beginning in Visual Studio 2015, they are not available in the CRT. The secure versions of these functions, gets_s and _getws_s, are still available.* – kaylum Apr 21 '21 at 04:34
  • ```gets``` is dangerous to use in the code, because it expects consistent input. It was removed and is replaced with ```get_s```. It is vulnerable to buffer overflow attacks. If you want to learn more about it, watch the video made by computerphile where professor Mike Pound shows how a buffer overflow attack works. – iwrestledthebeartwice Apr 21 '21 at 04:41
  • 1
    [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/995714) – phuclv Apr 21 '21 at 05:23

0 Answers0