That is beacuse your
scanf()
call reads the input without the new line character generated when pressing the ENTER key. So this new line character is still in the input buffer. The
fgets()
call will read from the input until a new line character is present. But that is already there and so it returns immediately.
This has been asked before and multiple answers and solutions can be found in the web like at
c - fgets instructions gets skipped.Why? - Stack Overflow[
^].
In general you should not use
scanf()
for keyboard input. It is better to write an input routine using
fgets()
, parse the string to check if it matches the required format, and convert it to the destination type upon success (e.g. using
atoi()
or
strtol()
for integers).