Back to C Programming
2026-01-245 min read

6.1 Basic Arithmetic

Learn 6.1 Basic Arithmetic step by step with clear examples and exercises.

Title: Mastering Basic Arithmetic Operations in C Programming

Why This Matters

Understanding basic arithmetic operations is crucial for any C programmer as they form the foundation of almost every programming task. Mastering these operations will help you write efficient and error-free code, save you from common bugs that may occur during coding, and make it easier to understand more complex mathematical concepts in your programs.

Prerequisites

Before diving into the core concept, it is essential to have a good understanding of:

  1. C syntax basics (variables, constants, operators)
  2. Basic input/output using printf and scanf functions
  3. Control structures like if, else, for, and while loops
  4. Understanding data types in C (e.g., integers, floating-point numbers)
  5. Familiarity with the difference between assignment operator (=) and equality operator (==)
  6. Knowledge of C functions like sqrt() for square root calculation and abs() for absolute value
  7. Understanding the concept of type promotion rules in C
  8. Basic understanding of memory management and how variables are stored

Core Concept

In this section, we will discuss the five basic arithmetic operations in C: addition, subtraction, multiplication, division, and modulus (remainder). We'll also cover increment/decrement operators, type promotion rules, and common pitfalls.

Addition and Subtraction

Addition is performed using the + operator, while subtraction uses the - operator. For example:

int a = 5;
int b = 3;
int sum = a + b; // sum equals 8
int diff = a - b; // diff equals 2

Multiplication and Division

Multiplication is performed using the * operator, while division uses the / operator. For example:

int a = 5;
int b = 3;
int product = a * b; // product equals 15
float quotient = (float)a / b; // quotient equals 1.66667 (casting `a` to float for division)

Modulus Operator

The modulus operator, denoted by %, returns the remainder of a division operation. For example:

int a = 10;
int b = 3;
int rem = a % b; // rem equals 1 (10 divided by 3 has a remainder of 1)

Increment and Decrement Operators

The increment operator, denoted by ++, increases the value of a variable by 1. The decrement operator, denoted by --, decreases the value of a variable by 1. For example:

int a = 5;
a++; // a now equals 6
a--; // a now equals 5 again

Type Promotion Rules

Type promotion rules in C allow for mixed-type arithmetic operations, where smaller data types are promoted to larger ones. For example:

int a = 5;
char b = '3'; // '3' is an ASCII character with value 51
int sum = a + b; // sum equals 56 (5 promoted to int and '3' promoted to int)

Order of Operations

In C, arithmetic operations follow the standard order of operations (PEMDAS): Parentheses, Exponents, Multiplication and Division (from left to right), Addition and Subtraction (from left to right). For example:

int a = 5 + 2 * 3; // a equals 11 (multiplication is performed first)

Practice Example

Let's write a simple program that calculates the area of a rectangle using user input for length and width.

#include <stdio.h>

int main() {
int length, width;

printf("Enter the length: ");
scanf("%d", &length);

printf("Enter the width: ");
scanf("%d", &width);

int area = length * width;
printf("The area of the rectangle is: %d\n", area);

return 0;
}

Worked Example

Let's walk through a more complex example that involves multiple arithmetic operations, type promotion rules, and parentheses.

#include <stdio.h>

int main() {
int a = 5;
int b = 3;
int c = 2;
int d = 7;

int sum1 = a + b * c; // sum1 equals 16 (multiplication is performed first)
int prod = (a + b) * c * d; // prod equals 348 (parentheses are evaluated first)
int diff = a - b + c * d; // diff equals 20 (multiplication is performed before addition and subtraction from left to right)

printf("sum1: %d\n", sum1);
printf("prod: %d\n", prod);
printf("diff: %d\n", diff);

return 0;
}

Common Mistakes

  1. Forgetting to include the required header files (e.g., ``)
  2. Incorrect use of parentheses
  3. Mixing integer and floating-point numbers in a single expression without casting
  4. Using the assignment operator (=) instead of the equality operator (==) for comparison
  5. Forgetting semicolons at the end of statements
  6. Not handling potential division by zero errors
  7. Ignoring the order of operations when writing complex expressions
  8. Misunderstanding the behavior of the modulus operator with negative numbers
  9. Using variables before they are initialized
  10. Overlooking type promotion rules and their effects on arithmetic expressions
  11. Failing to consider the range of data types (e.g., integer overflow)

Practice Questions

  1. Write a program that calculates the perimeter and area of a square using user input for side length.
  2. Write a program that finds the larger of two numbers entered by the user.
  3. Write a program that determines whether a number is even or odd.
  4. Write a program that calculates the sum of all numbers from 1 to 100.
  5. Write a program that calculates the factorial of a given number (using recursion).
  6. Write a program that finds the greatest common divisor (GCD) of two numbers using Euclid's algorithm.
  7. Write a program that converts temperatures between Celsius, Fahrenheit, and Kelvin.
  8. Write a program that calculates the square root of a given number using Newton-Raphson method.
  9. Write a program that finds the smallest common multiple (SCM) of two numbers using the Euclidean algorithm.
  10. Write a program that calculates the average of an array of integers.

FAQ

Q: Why do I get a compilation error when using arithmetic operators?

A: Ensure you have included the correct header files (e.g., ``) and that your syntax is correct.

Q: How can I perform exponentiation in C?

A: Use the power operator, denoted by ^. For example: int result = 2 ^ 3;

Q: Why does my program crash when I use the modulus operator with negative numbers?

A: The modulus operator gives the remainder of a division operation. When one or both operands are negative, the sign of the result may not be as expected. Use absolute values if necessary.

Q: How can I avoid potential division by zero errors in my program?

A: Always check if the divisor is different from zero before performing a division operation.

Q: What is the difference between the assignment operator (=) and the equality operator (==)?

A: The assignment operator assigns a value to a variable, while the equality operator compares two values for equality.

Q: Why do I need parentheses in complex arithmetic expressions?

A: Parentheses help clarify the order of operations and prevent potential errors due to ambiguity.

Q: How can I handle large numbers in my C program?

A: Use data types like long long int or libraries like GNU Multiple Precision Arithmetic Library (GMP) for handling large numbers.

Q: What are type promotion rules in C, and why do they matter?

A: Type promotion rules allow for mixed-type arithmetic operations, where smaller data types are promoted to larger ones. Understanding these rules can help you write more efficient code and avoid unexpected results.