/* * Program to get and echo two lines of input, discarding input that won't fit * in the array meant to hold the line read. (Yes, it's silly, but it's an * example of reading input in a "safe" way.) */ #include #include #include void get_and_echo(void) { char text[20]; printf("enter some text\n"); fgets(text, sizeof text, stdin); if (strchr(text, '\n')) { /* TODO: remove trailing '\n' ? */ printf("you entered: %s", text); } else { printf("you entered: %s (and there was more input)\n", text); while (fgetc(stdin) != '\n'); } } int main(int argc, char *argv[]) { get_and_echo(); get_and_echo(); return EXIT_SUCCESS; }