Is the following switch statement correct? I mean can i use constant and character literal in one switch case statement? It works in code but I am asking from good practices standpoint.
switch(arg[1]) {
case '4':
printf("value is 4\n");
break;
case '6':
printf("value is 6\n");
break;
case 'M':
printf("value is M\n");
break;
default:
break;
}
It works in code but I am asking from good practices standpoint.
Yes, it's fine to use char
variables and constants in switch
statements. It's very common to see that, for example, to process command line arguments . char
is an integer type, and switch
works as well with char
as with any other integer type.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.