Simple Calculator in C Using Switch Statement – All 4 Operations

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:

  1. scanf("%lf %c %lf", &a, &op, &b) reads a = 15.0, op = '/', b = 4.0.
  2. The switch evaluates op. It matches case '/'.
  3. The division-by-zero guard checks b == 0.0 — false here, so execution continues.
  4. result = 15.0 / 4.0 = 3.75
  5. 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 double values and a char operator in one call. The spaces in the format string skip any whitespace (including newlines) between tokens, so 12 + 5 and 12+5 both work.
  • switch (op) — branches on the operator character. Each case handles one operator and ends with break to 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 double means 15 / 4 gives 3.75 rather than 3. 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 of if-else if blocks.
  • 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%.2f prints a double with exactly two decimal places, giving clean output regardless of the actual value.

Related Programs

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>