Learn C
#include "operators/op_arithmetic.h"

Arithmetic Operators in C

#concept

C arithmetic uses the familiar five: + - * / % (modulo). The critical rule is that / on two integers performs truncating integer division — 7 / 2 is 3, not 3.5 — while mixing in any floating operand promotes the whole expression. The % operator works only on integers and yields the remainder: the backbone of divisibility tests, wrapping indexes, and digit extraction.

Compound forms (+=, -=, *=, /=, %=) combine operation with assignment, and prefix/postfix ++ and -- step by one with different timing: ++i increments before use, i++ after.

#how it works
1

The five operators

+ - * / % cover addition through remainder. % accepts integers only — 7.5 % 2 refuses to compile.

2

Integer division truncates

Both operands int means the fraction is discarded toward zero: 7 / 2 == 3 and -7 / 2 == -3.

3

Mixed types promote

One double operand lifts everything to double: 7 / 2.0 == 3.5. The usual fix for accidental truncation is casting one side.

4

Compound and increment forms

x += 2 beats x = x + 2 for brevity; ++i vs i++ differ only in when the increment lands relative to the expression.

#example
#include <stdio.h>
 
int main(void) {
int total = 7, n = 2;
printf("%d\n", total / n);
printf("%d\n", total % n);
printf("%.1f\n", total / 2.0);
return 0;
}
Output
3
1
3.5
1total / n divides two ints with %d printing, truncating 3.5 down to 3.
2total % n yields the remainder 1 — 7 is odd, exactly what % tests.
3total / 2.0 promotes to double division, printed with one decimal as 3.5.
#use cases
Averaging with (a + b) / 2.0 instead of / 2 to avoid silent truncation
Testing even/odd, wrapping circular buffers, extracting digits with %
Stepping counters and indexes with ++ in tight loops
Accumulating running totals concisely with += inside loops

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.