0

Doubt in switch case

#include<stdio.h> #include<stdlib.h> int main() { char ch; while(1) { printf("enter char value:"); scanf("%c",&ch); switch(ch) { case 'a': printf("a\n"); break; case 'b':printf("b\n"); break; case 'e':exit(0); break; default:printf("chose valid char\n"); } } } Output: enter char value:a a enter char value:chose valid char enter char value:e -------------------------------- Process exited after 49.66 seconds with return value 0 Press any key to continue . . . Friends my doubt is ....I have given a as input ....it's fine. But after that, why is it going to default.........?

25th May 2019, 6:39 PM
malepu rakesh
malepu rakesh - avatar
15 Respuestas
+ 4
~ swim ~ and Anna I tried it on my mobile compiler it's executing the default
25th May 2019, 6:51 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 4
cxxdriod ~ swim ~
25th May 2019, 7:02 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 4
~ swim ~ he is using an infinite loop, that's true, the newline is causing that
25th May 2019, 7:11 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 3
please put the code on code play ground and link it here edit: it seem the while loop triggers that, but I don't actually know why
25th May 2019, 6:42 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 1
It reads whatever you enter as a separator and treats it as a char. For example if your input is "a a a e", it will read the first 'a' and execute case 'a'. Then it will read ' ' (the space used as separator) and execute the default case. Then it will read an 'a' again, and another space etc.
25th May 2019, 6:50 PM
Anna
Anna - avatar
+ 1
I got that output in dev c
25th May 2019, 6:58 PM
malepu rakesh
malepu rakesh - avatar
+ 1
No, In PC .....Dev C++
25th May 2019, 7:02 PM
malepu rakesh
malepu rakesh - avatar
+ 1
Thank you friends😊
26th May 2019, 4:13 PM
malepu rakesh
malepu rakesh - avatar