Skip to content

Arithmetic and Integer Division

The Course
•
30 min study

You studied for 95 minutes and want the report to say how long that was in hours. On a calculator, 95 divided by 60 is 1.583. In C, 95 / 60 is 1. That is not a bug in C; it is a rule, and once you know it you can use it on purpose. Integer division gives you whole hours, the remainder operator gives you leftover minutes, and a floating-point operand gives you the fraction. This lesson is about choosing the one you mean.

Predict and run

Predict both lines. Pay attention to which division uses 60 and which uses 60.0.

Predict before running C
#include <stdio.h>

int main(void) {
    int minutes = 95;
    printf("%d hours %d minutes\n", minutes / 60, minutes % 60);
    printf("%.2f hours\n", minutes / 60.0);
    return 0;
}
Check your prediction
Text
1 hours 35 minutes
1.58 hours

minutes / 60 divides two integers, so the result is an integer: the fraction is discarded. minutes % 60 is the remainder left over, 35. minutes / 60.0 has a double operand, so the whole division happens in floating point and keeps the fraction; %.2f rounds it to two places for printing. Try 120 and then 59 and predict all three numbers before running.

The type of an expression comes from its operands

C decides how to compute an operator from the types of its two operands, not from where the result is going. This is the key idea of the lesson.

  • int / int is integer division. The result is truncated toward zero: 7 / 2 is 3 and -7 / 2 is -3.
  • If either operand is a double, the other is converted and the result is a double: 7 / 2.0 is 3.5.
  • % works only on integers and gives the remainder that goes with integer division, so (a / b) * b + a % b always equals a (when b is not zero).

A very common mistake is to store an integer result in a double and expect the fraction to come back. It cannot, because it was thrown away before the assignment happened:

Too late to recover the fraction C
#include <stdio.h>

int main(void) {
    int minutes = 95;
    double wrong = minutes / 60;
    double right = minutes / 60.0;
    printf("%.2f %.2f\n", wrong, right);
    return 0;
}
Text
1.00 1.58

The right side of wrong was computed as int / int, giving 1, and only then converted to 1.0. Put the double into the division itself.

Precedence and grouping

*, / and % are applied before + and -, and operators of the same level go left to right. So 10 + 20 / 2 is 20, not 15. Parentheses make the grouping you want explicit. When you are averaging two sessions, the parentheses are the difference between right and wrong:

Parentheses change the answer C
#include <stdio.h>

int main(void) {
    int first = 10;
    int second = 20;
    printf("%d\n", first + second / 2);
    printf("%d\n", (first + second) / 2);
    return 0;
}
Text
20
15

When you are not sure which operator binds first, add parentheses. They cost nothing and they make the intent visible to the next reader.

Format a clock time

Integer division and remainder split one quantity into parts. To print 125 minutes as a clock time you need hours and minutes, and the minutes must always show two digits. %02d prints an integer at least two characters wide, padding with leading zeros.

Pad the minutes C
#include <stdio.h>

int main(void) {
    int minutes = 125;
    printf("%d:%d\n", minutes / 60, minutes % 60);
    printf("%d:%02d\n", minutes / 60, minutes % 60);
    return 0;
}
Text
2:5
2:05

The first line looks like two hours and fifty minutes. The arithmetic was right; the formatting misled the reader.

Two operations C does not define

Some arithmetic has no defined result, and C does not stop the program to tell you. Dividing an integer by zero is undefined behavior: the standard makes no promise at all. It often crashes, but the compiler is also allowed to assume it never happens and reorganize your code around that assumption. Always make sure a divisor cannot be zero before you divide.

Signed integer overflow is the same. An int has a limited range, at least up to 32767 and commonly up to 2147483647. A result outside that range is undefined behavior, not a reliable wraparound to a negative number. The minutes in this course stay far below the limit, and on Day 6 you will see a parser that rejects large input before any overflow can happen.

Practice

Exercise

Your turn

A study log has int total = 245; minutes. Print it as a clock time, then as fractional hours with two decimals, then the average of three equal sessions as a whole number of minutes with the remainder.

Text
4:05
4.08 hours
81 minutes each, 2 left over
Your workspace C
#include <stdio.h>

int main(void) {
    int total = 245;
    // Write your solution here.
    return 0;
}
Hint 1: the idea

All three lines describe the same total. Integer division and % split it into whole parts and a remainder; a double operand keeps the fraction.

Hint 2: a first step

Write the clock line with total / 60 and total % 60, using %02d for the minutes, and check it prints 4:05 before adding the next line.

Show one solution
C
#include <stdio.h>

int main(void) {
    int total = 245;
    printf("%d:%02d\n", total / 60, total % 60);
    printf("%.2f hours\n", total / 60.0);
    printf("%d minutes each, %d left over\n", total / 3, total % 3);
    return 0;
}
Text
4:05
4.08 hours
81 minutes each, 2 left over

Each line chooses its arithmetic on purpose: integer division for whole hours and whole minutes per session, % for what is left, and 60.0 when the fraction matters. You can check the last line yourself: 81 times 3 is 243, plus 2 is 245. In the checkpoint you will combine variables and arithmetic into a small report.

Was this article helpful?

Your response is saved on this device.