This is broken code.
The programmer uses that getchar()
to clear the unread newline from the input buffer after the previous *scanf()
. It's a common pattern.
Note that if the next input is *scanf( "%d" )
or *scanf( "%f" )
or really any *scanf()
beginning with a conversion other than %c
, %[
and %n
, leading whitespace will be skipped anyway, which would make the getchar()
redundant.
But more importantly:
Don't use gets()
, ever. There is no way this function can be protected against buffer overflow, and it has been removed from newer versions of the language standard for exactly that reason. Use fgets()
instead.
Secondly:
If you are using *scanf()
to read user input (1), at least check the return value. If the user entered something that didn't match expectations, input matching will have failed, leaving the arguments (e.g. v_empregados[i].nome
) uninitialized.
(1) *scanf()
is good when reading back formatted data that has been written previously by your own application. But if there is any chance that the input might not be in the expected format (like when asking the user for input), the abilities to recover gracefully from that condition are limited. The general advice here is to use fgets()
to read a full line of input, and then parse that in-memory using e.g. strtol()
or strtod()
, which are superior in many ways. You can also go back and try a different parsing as often as you like.