{ }codelper

Learn › C

Basics · 04

Arithmetic and division

Add, subtract and multiply work the same everywhere — division doesn't.

+ - * work the same everywhere, but division and the remainder (%) differ between languages.
The example below takes two numbers and prints their sum, difference, product, quotient, remainder and fractional division.

#include <stdio.h> int main(void) {    int a, b;    scanf("%d %d", &a, &b);    printf("%d\n", a + b);    printf("%d\n", a - b);    printf("%d\n", a * b);    printf("%d\n", a / b);    printf("%d\n", a % b);    printf("%g\n", (double)a / b);    return 0;}
stdin
7 2
stdout
9
5
14
3
1
3.5

Key points

  • In some languages integer ÷ integer is an integer: 7 / 2 → 3 (the fraction is dropped).
    Languages with a single number type give 3.5 right away.
  • For a fractional result, convert one side to a floating-point number before dividing.
  • With negative numbers, languages split into two camps: those that truncate toward zero (-3.5 → -3) and those that floor (-3.5 → -4).
  • Integers have a limit.
    When they overflow, you may get a wrong value with no error at all.
  • Dividing by zero stops some languages with an error; others quietly give Infinity.

How each language writes it

Python// is the quotient (7 // 2 → 3), / always gives a float (7 / 2 → 3.5; even 6 / 2 is 3.0).
Negatives floor.
Integers never overflow.
Dividing by zero raises ZeroDivisionError
JavaScriptOne number type, so 7 / 2 → 3.5.
The quotient is Math.trunc(a / b); Math.floor differs for negatives (Math.floor(-3.5) → -4).
Beyond 2^53 the last digits go wrong (9007199254740993 → 9007199254740992).
Dividing by zero gives Infinity
TypeScriptSame as JavaScript
JavaInteger ÷ integer is an integer; for a fraction use (double)a / b → 3.5.
Truncates toward zero.
An int overflow silently wraps around; use long for big numbers.
Integer division by zero throws ArithmeticException
C#Same as Java.
Integer division by zero throws DivideByZeroException
C++Integer ÷ integer is an integer; for a fraction use (double)a / b.
Truncates toward zero.
Signed overflow and division by zero are undefined behavior.
Use long long for big numbers
CSame as C++
CraykonOne number type, so 7 / 2 → 3.5.
The quotient is Mathf.Quot(a, b), floor is Mathf.Floor.
% truncates toward zero.
Beyond 2^53 the last digits go wrong, and dividing by zero gives Infinity

Common mistakes

Converting after dividingJava · C# · C · C++

int a = 7, b = 2;
double d = (double)(a / b);

→ 3.0 — the integer division inside the parentheses finishes first

Convert before dividing: (double)a / b → 3.5.

Checking odd numbers with x % 2 == 1Java · C# · C · C++ · JavaScript · TypeScript · Craykon

int x = -3;
printf("%d\n", x % 2);

→ -1 — so x % 2 == 1 is false and negative odd numbers are missed

Write x % 2 != 0.
It is safe in every language.

int overflowJava · C# · C · C++

int x = 2147483647;
x = x + 1;

→ Java and C# silently give -2147483648. In C and C++ it is undefined behavior

Use long (long long in C and C++) for big numbers.

How languages differ
Negative division differs between languages.
Python gives -7 // 2 → -4 and -7 % 2 → 1 (floor), while C, C++, Java, C#, JavaScript (Math.trunc) and Craykon (Mathf.Quot) give quotient -3 and remainder -1 (truncate toward zero).

Practice