19th Jan 2022, 1:54 AM
ARAVIND DUDE
ARAVIND DUDE - avatar
5 Respostas
+ 7
The switch statement only allows CHAR and INT data types. Char refers to a single letter, symbol, number while String refers to set of characters. '&' -> character "&&" -> string "=>" -> string
19th Jan 2022, 2:44 AM
Simba
Simba - avatar
+ 4
It's better to use if-else statements for this kind of things. Somehow, https://stackoverflow.com/questions/4014827/how-can-i-compare-strings-in-c-using-a-switch-statement
19th Jan 2022, 3:39 PM
Simba
Simba - avatar
+ 3
In C, switch is only used with integers and characters that it converts to numbers, not with strings. You need to bring the strings to a number and only then use switch. #include <stdio.h> int main() { int a,b,c; char ch; printf("\n enter operators (&,|,!):\n"); scanf("%c",&ch); printf ("\n enter two operands\n"); scanf("%d%d",&a,&b); switch (ch) { case '&': //debug c=a&&b; printf ("c=%d",c); break; case '|': //debug c=a||b; printf ("c=%d",c); break ; case '!': c=a!=b; //debug, (or: c=!a) printf ("c=%d",c); break; default : printf ("\n enter only logical operator\n"); } }
19th Jan 2022, 2:51 AM
Solo
Solo - avatar
+ 2
Also, in operands input, use a separator, like a space, in scanf format spec. This way, the command can distinguish the two operands.
19th Jan 2022, 4:06 AM
Emerson Prado
Emerson Prado - avatar
0
How can we use strings in switch statement ?? can anyone give example of this code..
19th Jan 2022, 6:06 AM
ARAVIND DUDE
ARAVIND DUDE - avatar