16

This is out of curiosity. What is the maximum number of switch cases I can have in a single switch including the default: case. I mean like this:

switch(ch)
{
case 1:
//some statement
break;
case 2: 
//some statement
break;
.
.
.
.
case n:
//some statement
break;
default:
//default statement
}

My question is what is the maximum value that we can have here? Although this is not programatically significant, I found this a rather intriguing thought. I searched some blogs and found a statement here.

From a doc I have, it is said that:

Standard C specifies that a switch can have at least 257 case statements. Standard C++ recommends that at least 16,384 case statements be supported! The real value must be implementation dependent.

But I don't know how accurate this information is, can somebody give me an idea? Also what does it mean by implementation dependent? Suppose there is a limit like this, can I somehow change it to a higher or lower value?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Tom Thomas
  • 619
  • 2
  • 8
  • 24
  • The bit on implementation quantities in the C++ Standard is non-normative. That is, 16384 is only a polite suggestion, a standard conformant implementation could support much less cases. – K-ballo Dec 03 '13 at 16:09
  • Usually more than you want to type manually. – drahnr Dec 03 '13 at 16:10
  • _Implementation dependent_ means it _depends on the implementation_. An implementation of the language is the compiler, standard libraries and any required runtime support. So, all this says is _it depends on what compiler you're using_. So, you can only find out the upper limit for a specific compiler. – Useless Dec 03 '13 at 16:21

5 Answers5

18

The draft C++ standard Annex B (informative) Implementation quantities says (emphasis mine):

Because computers are finite, C++ implementations are inevitably limited in the size of the programs they can successfully process. Every implementation shall document those limitations where known. [...]

The limits may constrain quantities that include those described below or others. The bracketed number following each quantity is recommended as the minimum for that quantity. However, these quantities are only guidelines and do not determine compliance.

and includes the follow item:

— Case labels for a switch statement (excluding those for any nested switch statements) [16384].

but these are not hard limits only a recommendation on minimums.

The implementation is the compiler, standard library and supporting tools and so implementation dependent basically means for this case the compiler will decide what the limit is but it should document this limit. The draft standard defines implementation-defined behavior in section 1.3.10 as:

behavior, for a well-formed program construct and correct data, that depends on the implementation and that each implementation documents

We can see that gcc does not impose a limit for C:

GCC is only limited by available memory.

which should also cover C++ in this case and it looks like Visual Studio also does not place a limit:

Microsoft C does not limit the number of case values in a switch statement. The number is limited only by the available memory. ANSI C requires at least 257 case labels be allowed in a switch statement.

I can not find similar documentation for clang.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
4

Your question is tagged C++, so per C++98 Annex B/1:

Because computers are finite, C++ implementations are inevitably limited in the size of the programs they can successfully process. Every implementation shall document those limitations where known. This documentation may cite fixed limits where they exist, say how to compute variable limits as a function of available resources, or say that fixed limits do not exist or are unknown.

And then Annex B/2:

The limits may constrain quantities that include those described below or others. The bracketed number following each quantity is recommended as the minimum for that quantity. However, these quantities are only guidelines and do not determine compliance.

So as long as the implementation documents what it's doing, ANY max number of case statements is allowed. The standard recommends 16384 in a following list however.

Mark B
  • 95,107
  • 10
  • 109
  • 188
3

Per the c99 standard, section 5.2.4.1 Translation limits says:

The implementation shall be able to translate and execute at least one program that contains at least one instance of every one of the following limits:13)

and includes the following line:

— 1023 case labels for a switch statement (excluding those for any nested switch statements)

Per c++98 standard, Annex B (informative) Implementation quantities says:

The limits may constrain quantities that include those described below or others. The bracketed number following each quantity is recommended as the minimum for that quantity. However, these quantities are only guidelines and do not determine compliance.

— Case labels for a switch statement (excluding those for any nested switch statements) [16 384].

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
8bitwide
  • 2,071
  • 1
  • 17
  • 24
  • Fixed up your quotes, it is better to cite sections since it makes comparing different versions easier. FYI, [this page](http://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents) has a good set of standards links. – Shafik Yaghmour Dec 03 '13 at 19:01
2

In theory the max number of cases a switch statement can have depends on the data type of the variable you use:

data_type x

switch(x)
{
...
}

for char, you have 256, for short you have 65536 ...and so on; the maximum number of values you can represent given that data_type.

However, the compiler has to generate code for this switch(statement), and to code it usually generates is something like

cmp(R1,$value)
IFT jmp _subroutine
cmp(R1,$value2)
IFT jmp _subroutine2
...

The more cases you add, the higher the pressure on the registers and the larger the code size gets. Since memory and registers are not infinite, and the compiler is human-written there has to be a limit - and that is what is meant by implementation dependent. Each compiler can permit a different number of cases for a switch statement.

Pandrei
  • 4,843
  • 3
  • 27
  • 44
1

Implementation dependant means, the behaviour is not defined by standard, it is the decision of the compiler. The C++ standard does not set a minimum value for how many labels a switch statement shall support.

mcserep
  • 3,231
  • 21
  • 36