Program doesn’t execute gets() after scanf(), even using fflush(stdin)

If flushing std doesn’t work, then try reading in the extra characters and discarding, as suggested here. This will work: #include <string.h> #include <stdio.h> int main(){ char nombre[10]; char mensaje[80]; int c; printf(“Type your name:\n”); scanf(“%9s”, nombre); while((c= getchar()) != ‘\n’ && c != EOF) /* discard */ ; printf(“Now, type a message:\n”); gets(mensaje); printf(“%s:%s”,nombre,mensaje); … Read more

fflush(stdin) function does not work

The fflush function does not flush data out of an input stream; it is instead used to push data buffered in an output stream to the destination. This is documented here. As seen in this earlier SO question, trying to use fflush(stdin) leads to undefined behavior, so it’s best to avoid it. If you want … Read more

Flushing buffers in C

Flushing the output buffers: printf(“Buffered, will be flushed”); fflush(stdout); // Prints to screen or whatever your standard out is or fprintf(fd, “Buffered, will be flushed”); fflush(fd); //Prints to a file Can be a very helpful technique. Why would you want to flush an output buffer? Usually when I do it, it’s because the code is … Read more

Using fflush(stdin)

Simple: this is undefined behavior, since fflush is meant to be called on an output stream. This is an excerpt from the C standard: int fflush(FILE *ostream); ostream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that … Read more