0

why is this not working? im new to C... the scanf function works just fine with other data types, its just the char thats not giving me the option to input a character

char grade;
printf("Enter your grade: ");
scanf("%c", &grade);
printf("Your grade is %c", grade);
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
hikkichuu
  • 1
  • 1
  • https://stackoverflow.com/questions/13542055/how-to-do-scanf-for-single-char-in-c – Blackgaurd Mar 15 '22 at 19:03
  • 4
    Do you call `scanf` before this snippet? Please edit your post to include a [MCVE](https://stackoverflow.com/help/mcve) – Gerhardh Mar 15 '22 at 19:04
  • ...because you'll be reading the previous newline. Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – Weather Vane Mar 15 '22 at 19:06
  • 1
    The code you show works fine if you put it into a `main` function. If it does not work for you, then it is due to some code you did not show. We cannot diagnose code you don't show. That is why you should add a MCVE as I mentioned above. You should read [The Tour](https://stackoverflow.com/tour) and [How-to-ask](https://stackoverflow.com/help/how-to-ask) for more information about what you should care when posting at StackOverflow – Gerhardh Mar 15 '22 at 19:15
  • "new to C" and "scanf function works just fine"; http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html – William Pursell Mar 15 '22 at 19:18
  • hikkichuu, Try `printf("Your grade is %d <%c>\n", grade, grade);` and report the output. – chux - Reinstate Monica Mar 15 '22 at 22:44

1 Answers1

0

It seems before entering the character there are inputs of other objects in your original program.

In this case you need to write

scanf(" %c", &grade);

Pay attention to the blank before the conversion specifier %c. It allows to skip white space characters.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335