+ 1

Why is gets() function dangerous to use ?

I was trying I/O examples when I encountered an error while using gets() function the error message says it's too dangerous to use. The code I used was just the example code it still shows the same error. #include <stdio.h> int main() { char a[100]; gets(a); printf("You entered: %s", a); return 0; }

14th May 2020, 12:00 AM
Ahsan
Ahsan - avatar
5 odpowiedzi
+ 6
The gets() function does not place any limit on the data that it receives. A user could type so many characters that the program would crash or the system could run out of memory. The fgets() function lets you specify the number of characters that can be taken from stdin and is considered a safe alternative.
14th May 2020, 2:36 AM
Runcible
+ 1
Are you on windows?
14th May 2020, 1:51 AM
Mark McGuire
Mark McGuire - avatar
+ 1
I am also facing the same problem even after writing fgets(), if possible please someone give me the code with the correct input so that I can compare
8th Jun 2020, 5:01 PM
SANI TAK
SANI TAK - avatar
+ 1
That's the output I am getting----> ./Playground/file0.c: In function 'main': ./Playground/file0.c:6:5: error: too few arguments to function 'fgets' 6 | fgets(a); | ^~~~~ In file included from ./Playground/file0.c:1: ../Playground/:570:14: note: declared here 570 | extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream) | ^~~~~
8th Jun 2020, 5:02 PM
SANI TAK
SANI TAK - avatar