What does this syntax mean for the switch case in C?

I saw some c code as follows: int check = 10:

switch(check) { case 1...9: printf("It is 2 to 9");break; case 10: printf("It is 10");break; } 

What does this mean case 1...9: :? standard?

+6
source share
2 answers

This is a GNU C extension called the case range.

http://gcc.gnu.org/onlinedocs/gcc/Case-Ranges.html

As noted in the document, you should put spaces between low and high range values.

 case 1 ... 9: statement; 

is equivalent to:

 case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: statement; 
+7
source

This is gcc extension , the easiest way to find out is with gcc , at least use the -pedantic argument:

 gcc -pedantic 

will warn:

 warning: range expressions in switch statements are non-standard [-pedantic] 

and if you want to check a specific standard, for example c99 , follow these steps:

  gcc -std=c99 -pedantic 

Also, this is not true:

 case 1...9: 

you need spaces between dots and numbers:

 case 1 ... 9: 

as noted in the document :

Be careful: create spaces around ... otherwise it may not be parsed correctly when you use it with integer values.

+1
source

Source: https://habr.com/ru/post/949644/


All Articles