Implementing a Basic Calculator in C


A basic calculator is a simple yet practical application that can perform arithmetic operations like addition, subtraction, multiplication, and division. In this guide, we'll walk you through the process of implementing a basic calculator in C, explain how it works, and provide sample code to illustrate its usage.

Designing the Calculator

Our basic calculator will be a console application that allows users to enter two numbers and choose an operation. The calculator will then perform the selected operation and display the result.

Sample Code

Let's explore the sample code for implementing the basic calculator in C:

#include <stdio.h>
int main() {
char operator;
double num1, num2;
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator);
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
switch (operator) {
case '+':
printf("Result: %.2lf\\n", num1 + num2);
case '-':
printf("Result: %.2lf\\n", num1 - num2);
case '*':
printf("Result: %.2lf\\n", num1 * num2);
case '/':
if (num2 == 0) {
printf("Error: Division by zero\\n");
} else {
printf("Result: %.2lf\\n", num1 / num2);
printf("Error: Invalid operator\\n");
return 0;


To use the calculator, follow these steps:

  1. Run the program.
  2. Enter an operator (+, -, *, /) when prompted.
  3. Enter two numbers when prompted.
  4. The program will calculate the result and display it.


Implementing a basic calculator in C is a practical exercise that helps you understand user input, arithmetic operations, and decision-making with switch statements. This guide introduced the concept of a basic calculator, explained how it works in C, and provided sample code to demonstrate its usage. As you continue your C programming journey, you can expand and enhance this calculator or create more advanced applications.