Topic 22 of 100 Operators in C Interactive Tutorial

Performing Calculations in C With Arithmetic Operators

10 mins read Last updated: 22 Aug 2026 18,700 views
C Compiler 0.00%

What is Performing Calculations in C With Arithm? Core Concept & Direct Summary

Core Objective & Problem Statement

How do arithmetic operators (`+`, `-`, `*`, `/`, `%`) perform mathematical computations in C?

Direct Answer Summary

Every program calculates. Whether it's a shopping cart totaling prices, a game updating scores, or a sensor averaging readings — underneath it all, five humble operators are doing the work.

C's arithmetic operators look exactly like the math you learned in school, and four of them behave exactly as you'd expect. But the fifth one — the mysterious%— turns out to be a secret weapon with uses far beyond remainders. And division? Division has a trapdoor that's caught every C programmer at least once.

In this guide, you'll master all five arithmetic operators with runnable examples, dodge the integer division trap, and learn the modulus tricks that show up in real code every single day.

Let's calculate.

How Does Performing Calculations in C With Arithm Work? In-Depth Mechanics & Architecture

The Five Arithmetic Operators

OperatorNameExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division10 / 33⚠️
%Modulus (remainder)10 % 31

In action:

main.c
Run Online
#include <stdio.h>
int main() {
    int a = 10, b = 3;
    printf("a + b = %d\n", a + b);
    // 13    printf("a - b = %d\n", a - b);   // 7    printf("a * b = %d\n", a * b);   // 30    printf("a / b = %d\n", a / b);   // 3  ← wait, what?!    printf("a %% b = %d\n", a % b);  // 1  (%% prints a literal %)
    return 0;
}

Four operators behave politely. The last two —/and%— deserve their own conversations.

⚠️ Division: The Trapdoor Every Beginner Falls Through

Here's the rule that breaks a thousand first programs:

When both operands are integers,/performs integer division — the fraction is discarded, never rounded.

main.c
Run Online
printf("%d\n", 10 / 3);
// 3   — the .333... vanishesprintf("%d\n", 9 / 10);
// 0   — yes, ZEROprintf("%d\n", -7 / 2);
// -3  — truncates toward zero

No rounding, no error, no warning.9 / 10is genuinely0, and C sees nothing wrong with that.

The Three Ways to Get Real Division

main.c
Run Online
int a = 10, b = 3;
printf("%f\n", a / 3.0);
// 3.333333 — make one operand a doubleprintf("%f\n", (double)a / b);
// 3.333333 — cast one operandprintf("%f\n", a / (float)b);
// 3.333333 — either type works

The momentoneoperand is a decimal type, implicit promotion kicks in and the division happens in floating-point. One.0, one cast — that's all it takes. (Full story in our guides onimplicit conversionandtype casting.)

One Rule You Can Never Break: Division by Zero

main.c
Run Online
int x = 10 / 0;
// 💥 undefined behavior — crash, garbage, or worse

C doesn't protect you here. Dividing by zero isundefined behavior— your program may crash, print garbage, or appear to work while being silently broken. Real programs guard every division:

main.c
Run Online
if (divisor != 0) {
    result = total / divisor;
} else {
    printf("Cannot divide by zero!\n");
}

% — Modulus: The Underestimated Superstar

The modulus operator returns theremainderof division:

main.c
Run Online
10 % 3 // 1  — because 10 = 3×3 + 117 % 5    // 2  — because 17 = 5×3 + 28 % 4     // 0  — divides evenly, nothing left

"Just the remainder" sounds boring — until you see what programmers actually do with it:

Trick 1: Even or Odd?

main.c
Run Online
int n = 42;
if (n % 2 == 0) {
    printf("%d is even\n", n);
    // 42 is even
}

A number with no remainder when halved is even. This one-liner appears in millions of programs.

Trick 2: Extract the Last Digit

main.c
Run Online
int number = 12345;
printf("Last digit: %d\n", number % 10);
// 5

% 10peels off the last digit — the foundation of digit-counting, palindrome-checking, and number-reversing programs everywhere.

Trick 3: Wrap-Around Logic

main.c
Run Online
int position =(current + 1) % 7;
// days of week: 0-6, wraps to 0 after 6

Modulus creates cycles — circular buffers, rotating turns, clock arithmetic (14:00 + 12 hours = 2:00 because(14 + 12) % 12 = 2).

Trick 4: Divisibility Checks

main.c
Run Online
if (year % 4 == 0) {
    /* possibly a leap year */
}
if (n % 5 == 0) {
    /* divisible by 5 */
}

⚠️One limitation:%works onintegers only.10.5 % 3is an error — for floating-point remainders, usefmod()frommath.h.

[IMAGE 2 — Infographic: "4 Modulus Superpowers"]Design:Four tiles — "Even/Odd? n%2", "Last digit? n%10", "Wrap around? n%size", "Divisible? n%x==0" — each with a mini icon.Highly shareable.Alt text:Modulus operator in C tricks - even odd check, last digit, wrap around and divisibility

Operator Precedence: Who Calculates First?

What does2 + 3 * 4produce?14— not 20. Multiplication and division outrank addition and subtraction:

main.c
Run Online
Precedence(high → low):
1. * / %(multiplicative — evaluated first, left to right)
2. + -(additive — evaluated second, left to right)
main.c
Run Online
int r1 = 2 + 3 * 4;
// 14  — multiplication first
int r2 =(2 + 3) * 4;
// 20  — parentheses override
int r3 = 100 / 10 * 5;
// 50  — same rank? left to right
int r4 = 100 /(10 * 5);
// 2   — parentheses change everything

The professional habit:when an expression mixes operators, parenthesize for clarity — even when you know the precedence.(a + b) * cis never wrong and never needs a second look.

Unary Plus and Minus: The Solo Versions

The+and-operators also work on single operands:

main.c
Run Online
int temp = -15;
// unary minus — negative value
int flipped = -temp;
// 15 — unary minus flips the sign
int pointless = +5;
// unary plus — legal but does nothing

Unary minus is genuinely useful; unary plus exists mostly for symmetry (and to confuse readers).

A Complete Working Example: The Mini Calculator

Everything together in a real program:

main.c
Run Online
#include <stdio.h>
int main() {
    double num1 = 25.5, num2 = 4.0;
    printf("=== Mini Calculator ===\n");
    printf("%.1f + %.1f = %.2f\n", num1, num2, num1 + num2);
    printf("%.1f - %.1f = %.2f\n", num1, num2, num1 - num2);
    printf("%.1f * %.1f = %.2f\n", num1, num2, num1 * num2);
    if (num2 != 0) {
        printf("%.1f / %.1f = %.2f\n", num1, num2, num1 / num2);
    }
    // integer bonus round
    int a = 25, b = 4;
    printf("\nInteger bonus: %d %% %d = %d (remainder)\n", a, b, a % b);
    printf("%d is %s\n", a,(a % 2 == 0) ? "even" : "odd");
    return 0;
}

Output:

main.c
Run Online
=== Mini Calculator ===
25.5 + 4.0 = 29.50
25.5 - 4.0 = 21.50
25.5 * 4.0 = 102.00
25.5 / 4.0 = 6.38
Integer bonus: 25 % 4 = 1(remainder)
25 is odd

Notice the safety check before dividing, the doubles keeping precision, and the modulus earning its keep twice — remainderandeven/odd — in the same program.

The 5 Rules to Remember

  1. int / inttruncates—9 / 10is0. Cast or add.0for real division.
  2. Never divide by zero— guard every division with a check.
  3. %is integers-only— and it returns theremainder, not the percentage.
  4. * / %beat+ -— use parentheses when mixing.
  5. %is a superpower— even/odd, last digit, wrap-around, divisibility.

Runnable C Code Example & Practical Implementation

main.c
Run Online
#include <stdio.h>
int main() {
    int dividend = 29, divisor = 6;
    int quotient = dividend / divisor;
    int remainder = dividend % divisor;
    printf("29 / 6 = %d (Quotient)\
", quotient);
    printf("29 %% 6 = %d (Remainder)\
", remainder);
    return 0;
}

Key Takeaways, Best Practices & Memory Safety Rules

Best Practice & Key Takeaways
Always validate that the divisor is non-zero (if (divisor != 0)) before executing division or modulo operations.

Frequently Asked Questions & Technical Interview Insights

Technical Interview & Viva Questions

Frequently asked C programming interview questions
Q

Q: Can you apply `%` (modulo) directly to `float` or `double` operands in C?

A: No, `%` is strictly restricted to integer types (`char`, `int`, `long`). For floating-point remainder, use `fmod(x, y)` from ``.
Q

Q: What is the result of `-7 % 3` in C99 and later?

A: In C99+, the sign of the remainder matches the sign of the dividend (numerator), so `-7 % 3` is `-1`.

Related Tools, Tutorials & Authoritative References

Recommended Interactive Tools

Authoritative Standards & Citations