char data type needs space after quotation marks in scanf

2
#include <stdio.h>
#include <stdlib.h>

void main(){

  char elevador;
  int cod=0, a=0, b=0, c=0;


  while(cod == 0){
    printf("\nElevador utilizado (a/b/c)? ");
    scanf("%c", &elevador);

    switch(elevador){
      case 'a':
        a++;
        break;
      case 'b':
        b++;
        break;
      case 'c':
        c++;
        break;
      default:
        printf("opcao inválida!");
    }
  }
}

When I use the data type char and I use the scanf() do I need to give a space after the first quote?

In this way:

  

scanf ("% c", & elevator); < < Space after "_% c"

Well, if you do not use my program as code in the beginning will work "abnormal" can test, it will run like this:

    
asked by anonymous 19.06.2017 / 05:59

1 answer

3

Because scanf() treats the entry with a line break at the end and this may cause buffer problems. This space indicates that there will be a substitution for the line break that is spurious there. Usually it only gives a problem when there is a repetition of reading where the line break ends up for the next entry.

Related .

    
19.06.2017 / 06:30