- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - switch statement in C
![]() Share with a Friend |
C Programming - switch statement in C
switch Statement in C
The switch statement in C is used to handle multiple possible conditions based on the value of a variable. Unlike a series of if-else statements, the switch statement provides a more efficient and readable way to handle multiple possible cases when the conditions are related to a single expression or variable.
Syntax of switch Statement:
C
switch (expression) {
case value1:
// Code to be executed if expression is equal to value1
break;
case value2:
// Code to be executed if expression is equal to value2
break;
case value3:
// Code to be executed if expression is equal to value3
break;
default:
// Code to be executed if expression doesn't match any case
}
- expression: This is the variable or expression whose value is compared against each case.
- case valueN: If the expression matches valueN, the associated code block is executed.
- break: This statement ends the switch block after a matching case is found and executed. Without break, the program will "fall through" and continue checking the next cases.
- default: This is an optional case that is executed if none of the specified cases match the expression value. It acts like an "else" in if-else statements.
Example 1: Basic switch Statement
C
#include <stdio.h>
int main() {
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid day\n");
}
return 0;
}
Explanation:
- The variable day has the value 3.
- The switch statement checks the value of day and matches it with case 3.
- Since day is 3, it will print "Wednesday".
- The break ensures that no further cases are checked once a match is found.
Output:
Wednesday
Example 2: switch Statement Without break (Fall-through Behavior)
C
#include <stdio.h>
int main() {
int day = 2;
switch (day) {
case 1:
printf("Monday\n");
// No break here
case 2:
printf("Tuesday\n");
// No break here
case 3:
printf("Wednesday\n");
break;
default:
printf("Invalid day\n");
}
return 0;
}
Explanation:
- The value of day is 2.
- The switch matches case 2 and starts executing from there, but since there's no break after case 1, the program "falls through" and also executes case 2 and case 3.
- The output will be:
- "Tuesday"
- "Wednesday"
Output:
Tuesday
Wednesday
Example 3: Using default Case
C
#include <stdio.h>
int main() {
int month = 13;
switch (month) {
case 1:
printf("January\n");
break;
case 2:
printf("February\n");
break;
case 3:
printf("March\n");
break;
case 4:
printf("April\n");
break;
case 5:
printf("May\n");
break;
case 6:
printf("June\n");
break;
case 7:
printf("July\n");
break;
case 8:
printf("August\n");
break;
case 9:
printf("September\n");
break;
case 10:
printf("October\n");
break;
case 11:
printf("November\n");
break;
case 12:
printf("December\n");
break;
default:
printf("Invalid month\n");
}
return 0;
}
Explanation:
- The variable month has the value 13, which does not match any case in the switch statement.
- Since no case matches, the default case is executed, printing "Invalid month".
Output:
Invalid month
Example 4: switch with char Data Type
C
#include <stdio.h>
int main() {
char grade = 'B';
switch (grade) {
case 'A':
printf("Excellent\n");
break;
case 'B':
printf("Good\n");
break;
case 'C':
printf("Average\n");
break;
case 'D':
printf("Poor\n");
break;
default:
printf("Invalid grade\n");
}
return 0;
}
Explanation:
- The variable grade is 'B'.
- The switch statement checks for a match with the case 'B', and since it matches, it prints "Good".
Output:
Good
Important Notes:
- Fall-through Behavior: In C, if there is no break statement after a case, the program will continue executing the following cases. This is called "fall-through" behavior. It is often used intentionally, but it can lead to logical errors if not carefully managed.
- Case Sensitivity: The switch statement is case-sensitive, meaning case 'A' is different from case 'a'.
- default Case: The default case is optional but recommended, especially when handling unexpected values.
- Expression Type: The expression inside the switch can be of integer types, char, or enum. However, it cannot be a floating-point number or a string.
Summary of switch Statement:
- The switch statement is used for selecting one of many possible cases based on the value of an expression.
- It is more efficient than using multiple if-else conditions when comparing a single variable with many possible values.
- It works by matching the value of the expression to one of the case values, executing the corresponding block, and optionally using the break statement to stop further checks.
- The default case is executed if no other case matches the expression value.
