user1060517
user1060517

Reputation: 375

switch statement in C

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;
}

Upvotes: 0

Views: 130

Answers (1)

Caleb
Caleb

Reputation: 124997

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.

Upvotes: 3

Related Questions