Sun Studio 12 Update 1: C User's Guide

2.7 Case Ranges in Switch Statements

In standard C, a case label in a switch statement can have only one associated value. Sun C allows an extension found in some compilers, known as case ranges.

A case range specifies a range of values to associate with an individual case label. The case range syntax is:

case low ... high :

A case range behaves exactly as if case labels had been specified for each value in the given range from low to high inclusive. (If low and high are equal, the case range specifies only the one value.) The lower and upper values must conform to the requirements of the C standard. That is, they must be valid integer constant expressions (C standard Case ranges and case labels can be freely intermixed, and multiple case ranges can be specified within a switch statement.

Programming example:

enum kind { alpha, number, white, other }; 
enum kind char_class(char c); 
     enum kind result;
     switch(c) {
         case 'a' ... 'z':
         case 'A' ... 'Z':
             result = alpha;
         case '0' ... '9':
             result = number;
         case ' ':
         case '\n':
         case '\t':
         case '\r':
         case '\v':
             result = white;
             result = other;
     return result; }  

Error conditions in addition to existing requirements on case labels:

Note that if an endpoint of a case range is a numeric literal, leave whitespace around the ellipsis (...) to avoid one of the dots being treated as a decimal point.


 case 0...4;   // error
 case 5 ... 9; // ok