πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ English

Arithmetic Operators

A visual guide to C's arithmetic operators (+, -, *, /, %), implicit type conversion, and casts.

πŸ“– What to learn on this page
βœ… Must-know essentials
  • + - * / for arithmetic, % for remainder
  • int / int truncates (no decimals)
  • *// bind tighter than +/-
  • Use ( ) to override precedence
⭐ Read if you have time
  • Full operator precedence table
  • Type promotion with mixed int/double
  • Overflow behavior

Arithmetic Operators

Addition
a + b
Subtraction
a - b
Multiplication
a * b
Division
a / b
Modulo
a % b
Heads up: int / int truncates. 7/2 is 3, not 3.5.

Operator Calculator

int result
β€”
double result
β€”

Type Conversion & Casts

Mix different types in an expression and C performs a type conversion. Some conversions happen automatically; others you have to write out explicitly.

Implicit Conversion (Automatic)

When an expression mixes int and double, the int is automatically promoted to double. That's implicit conversion.
int a = 7;
double b = 2.0;
double result = a / b;  // a is auto-promoted to double β†’ 7.0 / 2.0 = 3.5
int / int
β†’ int
7 / 2 = 3 (truncated)
int / double
β†’ double
7 / 2.0 = 3.5
double / int
β†’ double
7.0 / 2 = 3.5
Rule: if either side is a double, the result is a double. Only when both sides are int do you get integer (truncated) division.

Explicit Cast (Manual)

Writing (target type) in front of an expression temporarily converts it to that type. This is called a cast.
Even with int / int, casting one side gives you a floating-point result.
int a = 7, b = 2;

// No cast β†’ integer division
printf("%d\n", a / b);           // 3 (truncated)

// With cast β†’ floating-point
printf("%f\n", (double)a / b);   // 3.500000
Cast syntax: (type) expression
int β†’ double
int x = 5;
double d = (double)x;  // 5.0
double β†’ int
double d = 3.7;
int n = (int)d;       // 3 (fractional part truncated)
Computing an average (typical case)
int sum = 17, n = 3;
double avg = (double)sum / n; // 5.666...
Common mistake: (double)(sum / n) does the integer division first (sum / n = 5), then casts the 5 to 5.0.
The cast has to happen before the division.

Cast Experiment

Try some values and compare the result with and without a cast.
a / b (int / int)
β€”
fractional part is lost
(double)a / b (with cast)
β€”
exact result

Common Situations Where a Cast Is Needed

SituationCodeWhy
Integer division, but you want a float result(double)a / bMake one side a double to force floating-point division
Computing an average(double)sum / countAvoid int / int truncation
malloc's return value(int *)malloc(...)Convert void* to the appropriate pointer type
Truncate a float toward zero(int)3.7 β†’ 3Drop the fractional part
Treat a char as a number(int)'A' β†’ 65Get the ASCII code

Check Quiz

int a = 10, b = 3;
printf("A: %d\n", a / b);
printf("B: %f\n", (double)a / b);
printf("C: %f\n", (double)(a / b));

What do A, B, and C print?

A: 3, B: 3.333333, C: 3.333333
A: 3, B: 3.333333, C: 3.000000
A: 3.333333, B: 3.333333, C: 3.000000
A: 3, B: 3.000000, C: 3.333333
Explanation:
A: int / int = int β†’ 10/3 = 3 (truncated).
B: (double)a / b β†’ 10.0 / 3 = 3.333333 (cast happens before the division, so it's floating-point).
C: (double)(a / b) β†’ (double)(3) = 3.000000 (integer division happens first β€” the position of the cast really matters!)

Try the Operators

operators.c
Output
Press "Run" to execute...
πŸ’‘ A few things to try

Related Lessons

Operators
Assignment & Increment
C assignment operators and increment (++) / decrement (--).
Operators
Comparison & Logical Operators
C comparison operators (==, !=, <, >) and logical operators (&&, ||, !).
Operators
Arithmetic Quiz
Quiz to check your understanding of C arithmetic operators.
← Previous lesson
Lesson 7: Quiz (scanf)
Next lesson →
Lesson 9: Quiz (Arithmetic)

Review Quiz

Check your understanding of this lesson!

Q1. What is 7 / 2 ? (both operands are int)

3.5
3
4

int / int drops the fractional part, so 7 / 2 = 3.

Q2. What is 7 % 2 ?

3
1
3.5

% is the modulo (remainder) operator. 7 / 2 is 3 remainder 1, so the result is 1.

Q3. What is (double)5 / 2 ?

2
2.5
2.000000

Casting to (double) forces floating-point division: 5.0 / 2 = 2.5.

Share this article
Share on X (Twitter) Share on Facebook Share on LINE Hatena Bookmark