To Whomever Can help Me;
Is it possible to use the Switch structure for characters? For example:
switch(char)
{
case 1:char = 'b'
.
.
.
I'm trying to do it, but having trouble. Is this even possible? Any help would be great! Thanks!
Dave
Printable View
To Whomever Can help Me;
Is it possible to use the Switch structure for characters? For example:
switch(char)
{
case 1:char = 'b'
.
.
.
I'm trying to do it, but having trouble. Is this even possible? Any help would be great! Thanks!
Dave
It is possible:
try thatCode:char choice;
printf("Enter a letter...:");
scanf("%c", choice);
switch (choice) {
case 'a': {
...
}
case 'b': {
...
}
}
Yes, chars and ints can be used in switches. But don't name a variable 'char' it's a keyword y'know? Also, the label cannot be a variable name, see below:
Code:
char a = 'a';
char b = 'a';
switch(a){
case b: a = 'x'; break; // <-- illegal.
case 't': a = 'z'; break;
case 12 : a = 'r'; break;
default: a = 'p';
}