Converting the GNU case range extension to standard C

前端 未结 4 1419
猫巷女王i
猫巷女王i 2021-01-12 08:09

The GNU case range extension allows case ranges in switch statements:

switch (value) {
    case 1 ... 8:
        printf(\"Hello, 1 to 8\\n\");
        break;         


        
4条回答
  •  借酒劲吻你
    2021-01-12 08:41

    If long long range you could do, a bit dirty but,

    switch(what) {
    case 1:
        /* do 1 */
        break;
    case 2:
        /* do 2 */
        break;
    default:
        if (what > 31 && what < 127) {
             /* do 32 to 126 */
        }
    }
    

    The best would probably be to remove the switch for an if all together.

    Be extremely strict with nesting. If you want the switch, for some reason, then better then the above would be:

    if (value > 31 && value < 127) {
      /* Do something */
    } else {
        switch (value) {
        case 1:
            ...
        }
    }
    

    Ach, sorry for edit again. This would be cleaner.

    if (value > 31 && value < 127) {
      /* Do something */
    } else if (value > 127 && value < 178) {
    
    } else if ( ...
    
    }
    
    switch (value) {
    case 1:
        ...
    }
    

提交回复
热议问题