-2
struct   Empregado  v_empregados[10];

main() {
    int i;
    for(i=0; i<10; i++){
        printf  (“Nome  e  apelidos?  “);
        gets  (v_empregados[i].nome);

        printf  (“Idade?  “);
        scanf(“%d”,&v_empregados[i].idade);
        getchar();

        printf(“Domicilio?  “);
        gets  (v_empregados[i].domicilio);

        printf  (“Salario?  “);
        scanf(“%f”,&v_empregados[i].salario);
        getchar();
}

In this code what is the purpose of using getchar()?

Spikatrix
  • 20,225
  • 7
  • 37
  • 83

3 Answers3

2

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.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
0

The getchar() is added in order to discard the \n(newline character) left over by the previous scanf() (Remember pressing Enter after entering a number for the scanf?).

Without the getchar()s, the \n will be left in the stdin and once the execution reaches gets, it sees the newline character in the stdin, consumes it, and thus, does not wait for further input.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
-1

It is done to consume the left out newline character \n after the previous scanf() gets executed.

This issue can also be solved by adding a space to the next scanf(), like in your code..

scanf(“ %d”,&v_empregados[i].idade);

NOTE

Don't use gets(), it has been deprecated.

However, most of your code is outside the main() function, so it won't get compiled.

Haris
  • 12,120
  • 6
  • 43
  • 70
  • In is case it does nothing though since its called outside of the `main()` and not in a function. – Magisch Oct 21 '15 at 09:36
  • @Magisch Thats not relevant i guess, its a small error.. However i will include that in the answer. – Haris Oct 21 '15 at 09:38
  • @Haris I agree, but it should be mentioned. – Magisch Oct 21 '15 at 09:38
  • 1
    `It is done to consume the left out newline character \n` - this is not correct, you don't need to do this if you're using `%d`, `%f` etc. – user4520 Oct 21 '15 at 09:40
  • @Michi yes, thats wat i meant. – Haris Oct 21 '15 at 09:40
  • 1
    "_most of your code is outside the `main()` function, so it won't get executed_" -- It won't even get compiled! – Spikatrix Oct 21 '15 at 09:41
  • 1
    "_This issue can also be solved by adding a space to the next `scanf()`_" -- That `scanf` will behave the same with or without the space. – Spikatrix Oct 21 '15 at 09:51
  • @CoolGuy, i guess m mixing things up here, Does this solves problems only for `char` inputs. – Haris Oct 21 '15 at 09:51
  • @Haris What? Could you explain? – Spikatrix Oct 21 '15 at 10:02
  • @CoolGuy *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.* Taken from an answer above. This i didn't know. – Haris Oct 21 '15 at 10:04