Are Elipses in case statements standard C/C++

前端 未结 3 1836
南旧
南旧 2020-12-03 14:36

I was browsing some code in the linux kernel and I came across the statements like case \'0\' ... \'9\':

To try this out I created the test program belo

相关标签:
3条回答
  • 2020-12-03 14:55

    This is a GCC extension to C, mentioned in this answer to what is basically a duplicate question, and confirmed in the GCC documentation.

    0 讨论(0)
  • 2020-12-03 14:57

    That's an extension. Compiling your program with -pedantic gives:

    example.cpp: In function ‘int main()’:
    example.cpp:9: error: range expressions in switch statements are non-standard
    example.cpp:12: error: range expressions in switch statements are non-standard
    

    clang gives even better warnings:

    example.cpp:9:12: warning: use of GNU case range extension [-Wgnu]
        case 0 ... 10:
               ^
    example.cpp:12:13: warning: use of GNU case range extension [-Wgnu]
        case 11 ... 100:
                ^
    
    0 讨论(0)
  • 2020-12-03 15:00

    That is the case range extension of the GNU C compiler, it is not standard C or C++.

    0 讨论(0)
提交回复
热议问题