01. Arithmetic Operators in C

🟢 Difficulty: Beginner | Addition, Subtraction, Multiplication, Division & Modulo

📖 Definition

Arithmetic operators in C are binary and unary mathematical symbols used to compute arithmetic operations on numerical operands (integers and floating-point numbers). C provides five binary arithmetic operators: + (addition), - (subtraction), * (multiplication), / (division), and % (modulo remainder).

🎯 Why It Is Used

  • Basic Calculations: Computing totals, averages, geometry formulas, and array index offsets.
  • Remainder Calculations: Modulo (%) is used to check even/odd numbers, cycle through circular array buffers, and extract digits.

⚙️ How It Works: Integer Division Truncation

When both operands in a division operation (/) are integers, C performs integer division, which truncates (discards) any fractional decimal part toward zero. To obtain a floating-point quotient, at least one operand must be explicitly cast or declared as a floating-point type (float or double).

📝 Syntax & Operator Table

Operator Operation Example Expression Evaluated Result
+Addition10 + 414
-Subtraction10 - 46
*Multiplication10 * 440
/Division10 / 42 (Integer truncation!)
%Modulo Remainder10 % 42 (Remainder after division)

🔀 Modulo Operator Rules

The modulo operator (%) operates only on integer operands in C (compiling 5.5 % 2.0 causes a syntax error). For floating-point remainder calculations, use fmod() from <math.h>.

🌱 Beginner Example

#include <stdio.h>

int main(void) {
    int a = 15, b = 4;

    printf("a + b = %d\n", a + b);
    printf("a - b = %d\n", a - b);
    printf("a * b = %d\n", a * b);
    printf("a / b = %d (Integer division)\n", a / b);
    printf("a %% b = %d (Remainder)\n", a % b);

    return 0;
}

🌼 Intermediate Example: Floating-Point Division & Even/Odd Test

#include <stdio.h>

int main(void) {
    int numerator = 17;
    int denominator = 5;

    // Explicit casting yields accurate floating-point division
    double precise_quotient = (double)numerator / denominator;
    int remainder = numerator % denominator;

    printf("17 / 5 (Precise)  : %.2lf\n", precise_quotient);
    printf("17 %% 5 (Remainder): %d\n", remainder);

    int test_num = 42;
    if (test_num % 2 == 0) {
        printf("%d is EVEN.\n", test_num);
    } else {
        printf("%d is ODD.\n", test_num);
    }
    return 0;
}

🌲 Advanced Example: Time Decomposition Utility (Hours, Minutes, Seconds)

#include <stdio.h>

void format_seconds(long total_seconds) {
    long hours = total_seconds / 3600;
    long remaining_after_hours = total_seconds % 3600;
    long minutes = remaining_after_hours / 60;
    long seconds = remaining_after_hours % 60;

    printf("%ld seconds = %02ldh:%02ldm:%02lds\n",
           total_seconds, hours, minutes, seconds);
}

int main(void) {
    format_seconds(3665); // Expect 1h:01m:05s
    format_seconds(86399); // Expect 23h:59m:59s
    return 0;
}

🔍 Line-by-Line Explanation

  • long hours = total_seconds / 3600;: Integer division determines the total whole hours.
  • long remaining_after_hours = total_seconds % 3600;: Modulo extracts leftover seconds after subtracting whole hours.
  • long minutes = remaining_after_hours / 60;: Calculates whole minutes from remaining seconds.
  • long seconds = remaining_after_hours % 60;: Modulo extracts the remaining final seconds.

💻 Expected Terminal Output

3665 seconds = 01h:01m:05s
86399 seconds = 23h:59m:59s

⚠️ Common Mistakes & Pitfalls

  • Division by Zero: Dividing by zero (e.g. x / 0 or x % 0) causes immediate runtime crash (Floating Point Exception / Division by Zero Signal).
  • Modulo with Floats: Writing 5.2 % 1.1 causes a compilation error. Use fmod(5.2, 1.1) from <math.h> instead.
  • Unexpected Integer Truncation: Expecting 5 / 2 to equal 2.5. Because both 5 and 2 are integers, 5 / 2 equals 2. Write 5.0 / 2 or (double)5 / 2.

📌 Important Rules & Limitations

  • In ISO C99/C11, integer division truncates toward zero (e.g., -7 / 3 == -2 and -7 % 3 == -1).
  • Precedence: *, /, % have higher operator precedence than + and -.

🌐 Multilingual Explanation: English

Arithmetic operators perform standard math. Integer division truncates fractional parts toward zero unless an operand is cast to double. Modulo (%) operates strictly on integers to compute division remainders.

🌐 Multilingual Explanation: Hindi (Roman Script)

Arithmetic operators se ganit ke calculations kiye jaate hain. Do integers ko divide karte waqt decimal part hat jata hai (truncation). Modulo % sirf integers ke sath kaam karta hai aur sheshfal (remainder) deta hai.

🌐 Multilingual Explanation: Marathi (Roman Script)

Arithmetic operators ganiti kriya kartat. Integer division kartaana decimal part truncate hoto. Remainder shakya karnyasathi modulo % vaparla jato jo fatka integer sathi chaluto.

❓ Practice Questions

  1. What is the result of 7 / 2 versus 7.0 / 2 in C?
  2. Why does 10.5 % 3.0 fail to compile in C?
  3. What happens when a program divides an integer variable by zero at runtime?

🚀 Practical Coding Challenge

Write a C program named currency_breakdown.c that takes an integer amount of money (e.g., $187) and uses integer division and modulo operators to break it down into the minimum number of $50, $20, $10, $5, and $1 bills.

🧭 Navigation & Breadcrumbs

← Module 05 Guide | Next: 02. Relational Operators →