can we use special characters in switch case in c
Yes, you can use special characters in a switch
case in C. The switch
statement in C allows you to compare the value of an expression against multiple constant values and execute different code blocks based on the matching case.
Here is an example of using special characters in a switch
case:
#include <stdio.h>
int main() {
char ch = '@';
switch (ch) {
case '@':
printf("You entered the at symbol.\n");
break;
case '#':
printf("You entered the pound symbol.\n");
break;
case '$':
printf("You entered the dollar symbol.\n");
break;
default:
printf("You entered a different character.\n");
break;
}
return 0;
}
In this example, the variable ch
is compared against different special characters using the switch
statement. If ch
matches any of the cases, the corresponding code block will be executed. In this case, if ch
is '@'
, it will print "You entered the at symbol."
Note: The special characters used in the case
labels should be enclosed in single quotes ('') to represent a character literal.
I hope this helps! Let me know if you have any further questions.