Originally Posted by
brack
why do you choose gets, puts instead of using scanf, printf which seem to be more classic functions? just asking...
gets and puts are about as "classic" as scanf and printf, in my opinion. However, gets should almost always be avoided as it cannot be used correctly to avoid a buffer overflow.
Actually, Char*Pntr's example does not need dynamic memory allocation at all. For example:
Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
while (1)
{
char buf[50];
puts("Enter some word, but not too many!");
if (fgets(buf, sizeof(buf), stdin))
{
/* Remove the trailing newline, if it is present. */
char *p = strchr(buf, '\n');
if (p)
{
*p = '\0';
}
else
{
/* Remove whatever is left in the input buffer. */
int c;
while ((c = fgetc(stdin)) != EOF && c != '\n');
}
puts(buf);
}
else
{
break; /* EOF (typically) */
}
}
return 0;
}