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 error. In C, using a integer literal does not cause 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.
- Difference between while(1) and while(0) in C language
- Using range in switch case in C/C++
- Switch Statement in C/C++
- How to print size of array parameter in C++?
- Print individual digits as words without using if or switch
- Functions in C/C++
- Arrays in C/C++
- How to write long strings in Multi-lines C/C++?
- C Language Introduction
- Interesting facts about switch statement in C
- Commonly Asked C Programming Interview Questions | Set 1
- Functions that are executed before and after main() in C
- What are the default values of static variables in C?
- A nested loop puzzle
- For Versus While
Improved By : SantanuBasak