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

Arithmetic Operators

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

Arithmetic Operators

Addition
a + b
Subtraction
a - b
Multiplication
a * b
Division
a / b
Modulo
a % b
Note: int / int is truncated. 7/2=3 (not 3.5)

Operator Calculator

int result
β€”
double result
β€”

Type Conversion & Casts

When you mix different types in an expression, C performs type conversion. Some conversions are automatic, others must be made explicit.

Implicit Conversion (Automatic)

When you mix int and double, the int is automatically promoted to double. This is called 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 double, the result is double. Only when both are int do you get integer (truncated) division.

Explicit Cast (Manual)

By writing (target type) before an expression, you can convert it temporarily. This is called a cast.
Even for 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) first performs the integer division sum / n = 5, then casts 5 to 5.0.
The cast must happen before the division.

Cast Experiment

Enter values and see how the result changes with and without a cast.
a / b (int / int)
β€”
fractional part lost
(double)a / b (with cast)
β€”
exact result

Common Situations Where a Cast Is Needed

SituationCodeWhy
Integer division, want a float result(double)a / bMake one side double β†’ floating-point division
Computing an average(double)sum / countAvoid int / int division
malloc return value(int *)malloc(...)Convert void* to the appropriate pointer type
Round 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 before divide β†’ float result)
C: (double)(a / b) β†’ (double)(3) = 3.000000 (integer division happens first β†’ cast position matters!)

Comparison & Logical Operators

Result is true (1) or false (0).
Equal
a == b
Not equal
a != b
Greater than
a > b
AND
a && b
OR
a || b
NOT
!a

Try the Operators

operators.c
Output
Press "Run" to execute...
Advertisement

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
scanf Quiz
Next Lesson β†’
Arithmetic Quiz

Check Your Understanding

Check your understanding of this lesson!

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

3.5
3
4

int / int truncates the fractional part. So 7 / 2 = 3.

Q2. What is 7 % 2 ?

3
1
3.5

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

Q3. What is (double)5 / 2 ?

2
2.5
2.000000

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

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

Recommended Books for This Lesson

Learning the concepts here + doing exercises in a book is very effective

πŸ“˜
Suffer Through C
MMGames
A classic introductory book for beginners. Careful explanations cover the fundamentals.
View on Amazon
πŸ“—
New Clear C Programming: Introduction
Bohyoh Shibata
Plenty of diagrams and practice problems. Widely used as a university textbook.
View on Amazon
πŸ“™
The C Programming Language (2nd Edition)
B.W. Kernighan, D.M. Ritchie
Known as K&R. The original C text. Perfect for the next step after basics.
View on Amazon

* These are affiliate links. Your purchases help support this site.