It's why c must be an int. A char can hold character values, but because EOF is a state of the stream rather than an actual character in the file, the variable you hold EOF in must be able to hold more values than a char. int is usually what's used.
Anyway, for replacing gets(), just use something like this.
Code:
char buffer[BUFSIZ], *p;
if(fgets(buffer, sizeof(buffer), stdin)) {
if((p = strchr(buffer, '\n'))) *p = 0;
printf("Hello, %s!\n", buffer);
}
else perror("Error reading line");
BUFSIZ is defined in <stdio.h>. It's usually at least 512, and I've seen it defined as 8192. fgets() doesn't remove the newline from the line like gets() does, so my code removes it manually with strchr() (from <string.h>). Lastly, the return value of fgets() is checked to make sure it worked.
A common idiom, to get every line possible, is to use
Code:
while(fgets(buffer, sizeof(buffer), stdin)) {
/* process buffer */
}
The loop exits when EOF or some other error is reached.
Also, one other note: when using sizeof() on actual variables instead of types like int, you can leave off the parentheses.
Code:
fgets(buffer, sizeof buffer, stdin);