Mouaz Tabboush
Mouaz Tabboush

Reputation: 25

C89 Switch case using a set

I'm trying to write a fuction which checks whether an input is a valid double value. since I recently learned about the switch/case/default feature I wanted to use it to solve the problem

switch(carrier[i]){
        case ("+" || "-") :  //case 1
            if(kvcase == closed){
                printf("Error! Invaled input\n");
            }
            else /*save sign and close case*/
                break;

        case '.' : 
            if(deccase == closed){
                printf("Error! Invaled input\n");
            }
            else /*save comma and close case*/
                break;

        case '[0-9]' :  //case 3
            break;

        case ' ':
            printf("staring with whitespace\n");
            break;

        default:
        printf("empty\n");
            break;


    }

To answer the question please igonre the resr of the code. The Question is only about the proper way to state a case.

1) is case 1 valid or do I have to seperate it into two different cases

2) is case 3 valied? I'm trying to use a set of numbers. so if char carrier[i] is a number case is matched. I know of isdigit() function, but I don't want to just get around the problem, unless it's impossible for switch case to work with a set.

Extra Info: - carrier ist of type char* and has been given a value - I'm compiling with gcc and the code needs to be in c89 standard, whatever that is.

Upvotes: 2

Views: 859

Answers (4)

chux
chux

Reputation: 154070

The Question is only about the proper way to state a case.
the code needs to be in c89 standard

Only 1 constant per case in standard C. No use of // comments in C89.

switch(carrier[i]) {
  case `+`: /* Fall through */  /*Comment not required, but good style */
  case `=`: 
    /* case 1 code here */
    break;
  case '.' : 
    if(deccase == closed) ...
    break;
  case '0' : /* Fall through */
  case '1' : /* Fall through */
  case '2' : /* Fall through */
  case '3' : /* Fall through */
  case '4' : /* Fall through */
  case '5' : /* Fall through */
  case '6' : /* Fall through */
  case '7' : /* Fall through */
  case '8' : /* Fall through */
  case '9' : 
    /* case 3 */
    ...
    break;
  case ' ':
    printf("staring with whitespace\n");
    break;
  default:
    printf("empty\n");
    break;
}

Upvotes: 2

0___________
0___________

Reputation: 67638

Switch case ranges are gcc extension

void foo(int ch)
{
    switch(ch)
    {
        case 'A' ... 'Z':
        case 'a' ... 'z':
            printf("'%c' is a letter\n", ch);
            break;
        case '0' ... '9':
            printf("'%c' is a digit\n", ch);
            break;
        case '+':
        case '*':
        case '-':
        case '%':
        case '/':
        case '^':
            printf("'%c' is a math operator\n", ch);
            break;
        default:
            printf("'%c' is something else\n", ch);
            break;
    }
}
int main()
{
    for(int x = 0; x < 50; x++)
    {
        foo(32+ rand() % 90);
    }
}

https://godbolt.org/z/UT-wkC

Upvotes: 1

Arkku
Arkku

Reputation: 42149

  1. No, at least in the sense that you mean: the value of "+" || "-" is 1, because at least one of operands ("+" and "-") of the logical-or operator || is non-zero.

  2. No, there no string or regular expression matching in the C switch, and with the single quotes (') the case is a multi-byte character.

Note that cases fall through to the next unless you break, so code like this works:

case '+':
case '-':
    (void) printf("+ or -\n");
    break;

(Note that you need to use single quotes for the '+' and '-' to make them characters. The "+" and "-" you had are strings, which do not compare equal to the characters, and cannot properly be matched by a switch anyway, see strcmp.)

Upvotes: 1

NoDataFound
NoDataFound

Reputation: 11959

For your question, the syntax is the following:

switch (expr) {
  case '1':
    printf("expr=1");
  case '2':
    printf("expr=2");
    break;
  case '3':
    printf("expr=3");
    break;
}

So:

  • Case 1 is invalid: use case '1': case '2':. Your case may compile but not give the expected result ("+"||"-" may resolve to 1).
  • Case 3 is invalid: you have to write all case (from '0' to '9').

If you want to minimize that, use a if:

if (c == '+' || c == '-') {
  // ...
} else if (c == '.') {
  // ...
} else if ('0' <= c && c <= '9') { // or isdigit(c)
  // ...
} else {
  // invalid character
}

Upvotes: 1

Related Questions