0

How many maximum cases we can write in switch case?

In C and C++ Language

9th Feb 2020, 9:50 AM
Avinash Maurya
Avinash Maurya - avatar
6 Réponses
+ 3
In practice you can use as many as you need, but there are limits. Atleast memory limit, writing switch statement billions times would require too much memory for the code to be saved. Because you can't write same case twice there is another limit, about how many different primitive values is there. Because eg. 97 and 'a' would be 1 same case, this would mean that we should only care of the values that can store most different values (floats cannot be used). long long can have 2^(8*8) different values. (from -2^63 to 2^63-1) unsigned long long can have 2^(8*8-1) more different values. (from 0 to 2^64-1) 2^(8*8) + 2^(8*8-1) = 2^64 + 2^63 = 2*(2^63) + 2^63 =3*2^63 Switch statement can have 3*2^63 + 1 different cases. (+ 1 comes from the default case)
9th Feb 2020, 10:13 AM
Seb TheS
Seb TheS - avatar
+ 1
As much as you want, but the code can become a bit unwieldy if you've got very long switch statements
9th Feb 2020, 10:13 AM
HNNX 🐿
HNNX 🐿 - avatar
0
You can use it as many as you like, but it is not a good thing, as your code will become long and boring, this can be avoided by using loops, functions, or several other methods.
9th Feb 2020, 10:48 AM
Rafik Abdelhak Nadir
Rafik Abdelhak Nadir - avatar
0
You can write as many as you want... Did you finish the course of loops for C++?
15th Apr 2020, 5:02 AM
SmallNinja2006
SmallNinja2006 - avatar
0
Of course, even compilers run on finite memory and time, so the maximum number of case labels is also limited by the implementation. In C++, https://timsong-cpp.github.io/cppwp/n4868/implimits#2.19 > Case labels for a switch statement ([stmt.switch]) (excluding those for any nested switch statements) [16 384]. Note that the numbers in square brackets are guidelines only and may not be actually met. If you are interested in more than just best practices, or if you are interested in code portability and how C/C++ implementations are standardized, you may want to look into language standards.
7th Aug 2024, 7:11 AM
fuseDestroyer
fuseDestroyer - avatar