+ 2

Why is there an error on "gets" in this code of a string while compiling?

#include <stdio.h> int main() { char s[30]; int x,c=0; printf("Enter the string to find length"); gets(s); for(x=0;s[x]!='\0';x++) { if(s[x]!=' ') { c++; } } printf("\n %d",c); return 0; }

14th Jan 2022, 12:47 PM
Sajid Kalam
Sajid Kalam - avatar
3 ответов
+ 3
gets is depreciated. use fgets
14th Jan 2022, 2:28 PM
Slick
Slick - avatar
+ 1
In my laptop using vscode with gcc engine compiler this code works fine. When a function is deprecated it still may work and sometimes compiler delivers a warning message. In C++ gets was excluded because it is dangerous. "gets" accept any char array of string and it can cause buffer to overflow by a billion character input. I believe this used to be a source of crashs in softwares. Anyway fgets is highly recommended because you can limit the number of characters passed by an input. Last, your code is a bit smart (I prefer straight simple code), but your code is totally fine in my humble view.
15th Jan 2022, 1:43 AM
Christina Ricci
Christina Ricci - avatar
+ 1
I published your code here: https://www.sololearn.com/post/1477883/?ref=app As you can see, it compiles fine.
15th Jan 2022, 2:15 AM
Christina Ricci
Christina Ricci - avatar