I have this noob question:
#include <stdio.h>
unsigned short int main() {
short int num_test = 0;
char car_test;
printf("Insert a number: ");
scanf("%hd", &num_test);
printf("Insert a character: ");
scanf("%c", &car_test);
return 0;
}
In the code above, car_teste
would receive the value '\n'
, right?
To prevent this, we should clean the keyboard buffer, right?
Well, instead of doing this:
char c;
while ((c = getchar()) != '\n' && c != EOF) {}
Why we "can't" use this solution, for example?
#include <stdio.h>
unsigned short int main() {
short int num_test = 0;
char car_test, kb_buff;
printf("Insert a number: ");
scanf("%hd", &num_test);
kb_buff = getchar();
printf("Insert a character: ");
scanf("%c", &car_test);
return 0;
}