the gets() function showing error on compile while trying | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

the gets() function showing error on compile while trying

while compiling this code below- #include <stdio.h> int main() { char a[100]; gets(a); printf("You entered: %s", a); return 0; } showing error as- You entered: solo learn ./Playground/file0.c: In function 'main': ./Playground/file0.c:6:5: warning: implicit declaration of function 'gets'; did you mean 'fgets'? [-Wimplicit-function-declaration] 6 | gets(a); | ^~~~ | fgets ../Playground/: /tmp/ccx1oKsh.o: in function `main': file0.c:(.text+0x15): warning: the `gets' function is dangerous and should not be used. please suggest why i'm seeing this error on compilation.

13th Jul 2020, 8:06 AM
Soumyajit Pal
Soumyajit Pal - avatar
1 Answer
0
Soumyajit Pal it is not error... Check the text you posted and it also shows that it is warning.... fgets is safe over array size compare to the gets... That is what Sololearn compiler is suggesting over the warning message. Fortunately, you have given size of array as 100 and it's far more than your input count of solo learn... What if input is having more characters than array size declared.Here gets is dangerous... Refer below sample code : int max = 3; char a[max]; fgets(a,max,stdin); printf("You entered: %s", a); Even if you enter solo learn as input, code will check and take only input as per given size of array and print only those much... This facility is not there in gets and hence it is not fool proof
3rd Aug 2020, 4:37 PM
Ketan Lalcheta
Ketan Lalcheta - avatar