1
#include<stdio.h>

int main()
{
    char a;

    do {
    printf("\n enter letter a,b or c: ");
    scanf("%c", &a);

    switch (a)
    {
    case  'a' :
        printf("\n one \n");
        break;
    case  'b' :
        printf("\n two \n");
        break;
    case  'c' :
        printf("\n three \n");
        break;
    default:
        printf("\n invalid input \n");
        break;
    }
    }while(a != 'd');
}

this code always runs default even there is a break;

but if u replace %c to %s, it works normally, i just want to understand why using %c does that or is there any other way to make it run perfectly with using %c

also using a=getch() works but i want to display the input without using printf to display it

Vavlo
  • 13
  • 3
  • 2
    Try using `" %c"` instead. Note the leading space in the format string. – Some programmer dude Oct 28 '22 at 10:22
  • 2
    As for what happens, I suggest you use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to see what value `a` really have in your current code. – Some programmer dude Oct 28 '22 at 10:24
  • 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 Oct 28 '22 at 10:24
  • 2
    *"If you replace %c to %s it works normally."* No, it is undefined behaviour. – Weather Vane Oct 28 '22 at 10:26
  • 1
    *"I want to understand what %c does"*. Some explanation: most of the format specifiers for `scanf` automatically filter leading whitespace, but `%c` and `%[]` and `%n` do not. Adding a space in front of the `%` instructs `scanf` to filter leading whitespace here too. The reason is those three specifiers allow you to read every character including whitespace, but a way is provided so they behave like `%d` and `%f` and `%s` where needed. – Weather Vane Oct 28 '22 at 10:27
  • 1
    If you want to understand `scanf`, sit down for two hours with the man page and a little test program. Don't use kludges such as `a=getch()`. They are not necessary, once you understand how whitespace is handled. – Weather Vane Oct 28 '22 at 10:28
  • Hint: change `printf("\n invalid input \n");` to `printf("\n invalid input , a= %d\n", a);` and see what happens. Hint 2: the `10` you will see is the code for the Enter key. – Jabberwocky Oct 28 '22 at 10:30
  • Or... just don't waste excessive amounts of time learning about `stdio.h`. It's easily one of most horrible libraries ever designed, all categories. It should at best be regarded as a quick & dirty debug library. There are very few programs that actually _need_ to take input from stdin instead of from for example command line parameters. In general, almost everyone stopped using console I/O some 20+ years ago. So apart from being intrinsically and horribly broken by design, this is also a quite irrelevant library to study when learning modern C programming. – Lundin Oct 28 '22 at 10:54

1 Answers1

0

When using scanf, you need to press enter key after the letter a/b/c to send the input. %c interprets your input as two characters. One for actual letter you inputted and the second for the enter key. It is this enter key character that passes through 'default' case. When you use getch(), everything works fine because you don't press the enter key

  • i saw a comment stating that using " %c" will work, and it did, would it be ok if you give me even a tiny knowledge of why did that work? even without explaining it thoroughly, just the key info – Vavlo Oct 28 '22 at 11:01
  • @Vavlo — the duplicate question explains it all. So do some of the comments. – Jonathan Leffler Oct 28 '22 at 13:37