In C++ switch statement, the expression of each case label must be an integer constant expression.
For example, the following program fails in compilation.
Putting const before i makes the above program work.
Note : The above fact is only for C++. In C, both programs produce an error. In C, using an integer literal does not cause an error.
Program to find the largest number between two numbers using switch case:
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Switch Statement in C/C++
- Nested switch case
- Using range in switch case in C/C++
- Interesting facts about switch statement in C
- Output of C programs | Set 30 (Switch Case)
- Is there any need of "long" data type in C and C++?
- What is the size_t data type in C?
- What is data type of FILE in C ?
- Data Type Ranges and their macros in C++
- Data type of character constants in C and C++
- Conversion of Struct data type to Hex String and vice versa
- Local Labels in C
- Difference between Type Casting and Type Conversion
- Print individual digits as words without using if or switch
- Continue Statement in C/C++
- goto statement in C/C++
- Break Statement in C/C++
- C/C++ if else statement with Examples
- C/C++ if statement with Examples
- return statement in C/C++ with Examples