C Operators, Integer Division and Type Conversion
C Operators, Integer Division and Type Conversion
A teacher divides 75 marks between two subjects and expects 37.5, but integer division produces 37. The operand types determine the calculation.
Operators combine values into expressions. When both division operands are integers, C divides with a result truncated toward zero. Converting an operand before division can choose floating-point arithmetic.
Explain
Predict integer division and choose a conversion before information is discarded.
Trace
Compute the average of 100 marks across three subjects to two decimal places.
Apply
Predict when integer division loses the fractional part.
Operators and Conversions: follow the data
Start
A teacher divides 75 marks between two subjects and expects 37.5, but integer division produces 37. The operand types determine the calculation.
Operation
Operators combine values into expressions. When both division operands are integers, C divides with a result truncated toward zero. Converting an operand before division can choose floating-point arithmetic.
Result
Whole: 37 Average: 37.5
Understand the operation
75 / 2 is integer division because both operands have integer type. Assigning that result to double afterward stores 37.0, not the discarded half. Writing 75.0 / 2 or (double)total / count changes the arithmetic before division.
Use parentheses to make important grouping explicit. == compares equality, = assigns, and % computes an integer remainder. Avoid expressions that both modify and read the same object without the sequencing required by C.
Read the complete C program
This complete C17 program uses a small school example. Read declarations before the statements that use their values. Each printf format describes how the corresponding value is written to standard output.
C17 · complete program
#include <stdio.h>
int main(void) {
int total = 75;
int subjects = 2;
printf("Whole: %d\n", total / subjects);
printf("Average: %.1f\n", (double)total / subjects);
return 0;
}
Expected output
Whole: 37
Average: 37.5
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int total = 75;
int subjects = 2;
printf("Whole: %d\n", total / subjects);
printf("Average: %.1f\n", (double)total / subjects);
return 0;
}
Integer operands first produce 37 after truncation.
The cast makes one operand double, so the second division produces 37.5 before printf formats it.
Follow the worked calculation
Read the starting values
Integer operands first produce 37 after truncation.
Perform the operation
The cast makes one operand double, so the second division produces 37.5 before printf formats it.
Check the observable result
Whole: 37 Average: 37.5
Whole: 37 Average: 37.5
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int total = 75;
int subjects = 2;
printf("Whole: %d\n", total / subjects);
printf("Average: %.1f\n", (double)total / subjects);
return 0;
}
Compute the average of 100 marks across three subjects to two decimal places.
Worked answer
Convert before dividing. The result is approximate and printf rounds its display to two fractional digits; it does not change the stored calculation into exact decimal arithmetic.
C17 · complete program
#include <stdio.h>
int main(void) {
int total = 100;
int subjects = 3;
printf("Average: %.2f\n", (double)total / subjects);
return 0;
}
Expected output
Average: 33.33
Run in C compiler →Recognize the important boundaries
Do not divide an integer by zero. The minimum signed value divided by -1 can also be unrepresentable. Validate real input before calculating.
A narrowing conversion may discard fractions or lose range. Do not cast merely to suppress a diagnostic when the value is not valid for the target type.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Operators combine values into expressions. When both division operands are integers, C divides with a result truncated toward zero. Converting an operand before division can choose floating-point arithmetic.
Continue with conditions and boolean tests.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.