#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
>Solution :
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