0
Doubt
This is the code , when I am running it , it says gets() is declared implicitly #include <stdio.h> #include <string.h> int main() { char s[20]; int i=0; printf("Enter A string"); get(s); while(s[i]!='\0') { if(s[i]=='') { s[i]=s[i+1]; s[i+1]=''; } i++; } printf("%s",s); return 0;
1 Respuesta
+ 2
`gets()` is a deprecated library function in C11 and no longer exists a prototype for it in <stdio.h> header file *. You should go for `fgets()` istead which provides a better safety measure compared to its old counterpart.
Syntax:
char * fgets ( char * str, int num, FILE * stream )
where `str` is the name of the character container, `num` is the size of the container, and `stream` refers to the desired input port (a file or stdin).
The above code transforms to
#include <stdio.h>
#include <string.h>
int main() {
char s[20];
int i=0;
printf("Enter A string");
fgets(s, 20, stdin);
while(s[i]!='\0')
{
if(s[i]==' ')
{
s[i]=s[i+1];
s[i+1]=' ';
}
i++;
}
printf("%s",s);
return 0;
}
Notice, a pair of quotation marks without any character between them (empty character constant) is an illegal form and therefore the compiler issues an error message.
______
* https://port70.net/~nsz/c/c11/n1570.html#Forewordp6
" removed the gets function (<stdio.h>) "