The fgets() function following the scanf() call probably cannot be skipped 1 . This is probably 1 returning immediately, finding a new line in the input stream.
Calling scanf() before fgets() almost always causes scanf() leave an unused newline ( '\n' ) line in the input stream, which is what fgets() looks like.
To mix scanf() and fgets() , you need to remove the new line left by calling scanf() from the input stream.
One solution for flushing stdin (including a new line) would be something like the following:
int c; while ((c = getchar()) != '\n' && c != EOF);
1 - It is difficult to be sure without seeing the actual code.
Or, as Jerry Coffin said in his comment below, you can use scanf("%*[^\n]"); . The directive "%*[^\n]" indicates scanf() to match those that are not newlines, and suppress the purpose of the conversion result.
scanf("%*[^\n]"); scanf("%*c");
From http://c-faq.com/stdio/gets_flush1.html :
The initial scanf() with "%*[^\n]" will either consume everything before, but not include a new line, or crash. The subsequent "%*c" (or plain old getchar() ) will use the newline, if any.
This last โifโ matters: the user may have signaled EOF. In this case, getchar() or scanf("%*c") can - this decision is left by people who write your compiler, either immediately return EOF, or return to the user for more input. If developers choose the latter, the user may need to click โstop this thingโ (^ D, ^ Z, mouse button, front panel switch, or something else) for extra time. It is annoying if nothing else.
Or, as Chris Dodd suggested in his comment below, you can use scanf("%*[^\n]%*1[\n]"); . The directive "%*[^\n]%*1[\n]" tells scanf() to match those that are not newline characters, and then match one newline and suppress the purpose of the conversion results.
scanf("%*[^\n]%*1[\n]");