Data type of case labels of switch statement in C++? Last Updated : 12 Aug, 2021 Comments Improve Suggest changes Like Article Like Report In C++ switch statement, the expression of each case label must be an integer constant expression. For example, the following program fails in compilation. CPP /* Using non-const in case label */ #include<stdio.h> int main() { int i = 10; int c = 10; switch(c) { case i: // not a "const int" expression printf("Value of c = %d", c); break; /*Some more cases */ } return 0; } Putting const before i makes the above program work. CPP #include<stdio.h> int main() { const int i = 10; int c = 10; switch(c) { case i: // Works fine printf("Value of c = %d", c); break; /*Some more cases */ } return 0; } 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: C #include<stdio.h> int main() { int n1=10,n2=11; // n1 > n2 (10 > 11) is false so using // logical operator '>', n1 > n2 produces 0 // (0 means false, 1 means true) So, case 0 // is executed as 10 > 11 is false. Here we // have used type cast to convert boolean to int, // to avoid warning. switch((int)(n1 > n2)) { case 0: printf("%d is the largest\n", n2); break; default: printf("%d is the largest\n", n1); } // n1 < n2 (10 < 11) is true so using logical // operator '<', n1 < n2 produces 1 (1 means true, // 0 means false) So, default is executed as we // don't have case 1 to be executed. switch((int)(n1 < n2)) { case 0: printf("%d is the largest\n", n1); break; default: printf("%d is the largest\n", n2); } return 0; } //This code is contributed by Santanu Comment More infoAdvertise with us Next Article Using Range in switch Case in C K kartik Follow Improve Article Tags : C Language C-Loops & Control Statements Similar Reads Switch Statement in C C switch statement is a conditional statement that allows you to execute different code blocks based on the value of a variable or an expression. It is often used in place of if-else ladder when there are multiple conditions.Example:C#include <stdio.h> int main() { // Switch variable int var = 5 min read Interesting facts about switch statement in C Prerequisite - Switch Statement in C Switch is a control statement that allows a value to change control of execution. C // Following is a simple program to demonstrate syntax of switch. #include <stdio.h> int main() { int x = 2; switch (x) { case 1: printf("Choice is 1"); break; cas 3 min read Output of C programs | Set 30 (Switch Case) Prerequisite - Switch Case in C/C++ Interesting Problems of Switch statement in C/C++ Program 1 C #include <stdio.h> int main() { int num = 2; switch (num + 2) { case 1: printf("Case 1: "); case 2: printf("Case 2: "); case 3: printf("Case 3: "); default: printf( 2 min read Using Range in switch Case in C You all are familiar with switch case in C, but did you know you can use a range of numbers instead of a single number or character in the case statement? Range in switch case can be useful when we want to run the same set of statements for a range of numbers so that we do not have to write cases se 2 min read Execute both if and else statements in C/C++ simultaneously Write a C/C++ program that executes both if-else block statements simultaneously. Syntax of if-else statement in C/C++ language is: if (Boolean expression) { // Statement will execute only // if Boolean expression is true } else { // Statement will execute only if // the Boolean expression is false 2 min read C++ if Statement The C++ if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not executed based on a certain condition. Let's take a look at an example:C++#include <iostream> using namespace std; int main() { int 3 min read Like