A simple calculator in C using switch statement reads an operator character (+, -, *, /) and dispatches to the correct arithmetic operation. The switch statement is ideal here: each operator maps to exactly one case, with a default branch catching anything unsupported. This program also demonstrates proper division-by-zero handling — an error that causes undefined behaviour if left unchecked.
How It Works — Step by Step
Given the input 15 / 4:
scanf("%lf %c %lf", &a, &op, &b)readsa = 15.0,op = '/',b = 4.0.- The switch evaluates
op. It matchescase '/'. - The division-by-zero guard checks
b == 0.0— false here, so execution continues. result = 15.0 / 4.0 = 3.75- Output:
15.00 / 4.00 = 3.75
C Program — Calculator Using Switch
/* Simple calculator in C using switch statement
* Compile: gcc -ansi -Wall -Wextra calc.c -o calc */
#include <stdio.h>
int main(void)
{
double a, b, result;
char op;
printf("Enter expression (e.g. 12 + 5): ");
scanf("%lf %c %lf", &a, &op, &b);
switch (op) {
case '+':
result = a + b;
printf("%.2f + %.2f = %.2f\n", a, b, result);
break;
case '-':
result = a - b;
printf("%.2f - %.2f = %.2f\n", a, b, result);
break;
case '*':
result = a * b;
printf("%.2f * %.2f = %.2f\n", a, b, result);
break;
case '/':
if (b == 0.0) {
printf("Error: division by zero.\n");
return 1;
}
result = a / b;
printf("%.2f / %.2f = %.2f\n", a, b, result);
break;
default:
printf("Error: unknown operator '%c'. Use +, -, *, /\n", op);
return 1;
}
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra calc.c -o calc
./calc
Sample Input and Output
Addition:
Enter expression (e.g. 12 + 5): 12 + 5 12.00 + 5.00 = 17.00
Subtraction:
Enter expression (e.g. 12 + 5): 10 - 3 10.00 - 3.00 = 7.00
Multiplication:
Enter expression (e.g. 12 + 5): 6 * 7 6.00 * 7.00 = 42.00
Division (non-integer result):
Enter expression (e.g. 12 + 5): 15 / 4 15.00 / 4.00 = 3.75
Division by zero:
Enter expression (e.g. 12 + 5): 5 / 0 Error: division by zero.
Code Explanation
- scanf(“%lf %c %lf”) — reads two
doublevalues and acharoperator in one call. The spaces in the format string skip any whitespace (including newlines) between tokens, so12 + 5and12+5both work. - switch (op) — branches on the operator character. Each
casehandles one operator and ends withbreakto prevent fall-through to the next case. - Division-by-zero guard — checked before dividing. In C, integer division by zero is undefined behaviour; floating-point division by zero produces
Inf, which is also undesirable. The explicit check catches both. - default branch — catches any character that is not
+,-,*, or/. Without it, invalid input would silently produce no output. - double instead of int — using
doublemeans15 / 4gives3.75rather than3. For a general-purpose calculator, floating-point is the right default.
What This Program Teaches
- switch statement with char — switch works on any integer type, including
char. Each operator character is matched exactly, making the code more readable than an equivalent chain ofif-else ifblocks. - break to prevent fall-through — without
break, execution continues into the next case. This is a common source of bugs in C switch statements. - Input validation before arithmetic — checking for division by zero before dividing is the correct pattern: always validate preconditions before calling an operation that can fail.
- Formatted output with %.2f —
%.2fprints adoublewith exactly two decimal places, giving clean output regardless of the actual value.
Related Programs
- Arithmetic Operators in C
- Simple Interest in C
- GCD and LCM in C
- Largest of 3 Numbers in C
- Numbers Divisible by 7 in C
Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
|
C Programming: A Modern Approach — K.N. King (India) |
(US)
Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.