C Tutorial
C Type Conversion
C converts types in expressions. Casts make the conversion explicit. Integer division truncates.
int divided by int truncates
When both operands of / are integers, C performs integer division. The fraction is discarded toward zero. 7 / 2 is 3, not 3.5. That is not rounding. The remainder is what % reports.
Example
#include <stdio.h>
int main(void) {
int a = 7;
int b = 2;
printf("int / int: %d\n", a / b);
printf("remainder: %d\n", a % b);
return 0;
}Output is 3 and 1. If you wanted 3.5, one operand must be floating-point before the division happens.
(double)a / b
A cast converts a value to another type. (double)a is 7.0. Dividing that by bpromotes b to double as well. The result is 3.5. Casting the quotient afterward is too late: (double)(a / b) is still 3.0, because the truncation already happened.
Example
#include <stdio.h>
int main(void) {
int a = 7;
int b = 2;
printf("cast first: %.1f\n", (double)a / b);
printf("cast after: %.1f\n", (double)(a / b));
printf("3.0 / 2: %.1f\n", 3.0 / 2);
return 0;
}Open this in /c/try. gcc compiles C. Swap in(double)(a / b) in your own print if you want to see the truncated path again.
Casts drop the fraction the other way too
Casting a double to int truncates toward zero. (int)3.9 is3. (int)-1.9 is -1. There is no rounding in the cast. Use a math library function if you need to round.
Example
#include <stdio.h>
int main(void) {
double x = 3.9;
double y = -1.9;
printf("(int)3.9 = %d\n", (int)x);
printf("(int)-1.9 = %d\n", (int)y);
return 0;
}Usual arithmetic conversions
In a mixed expression, C converts the narrower type toward the wider one. An int plus adouble becomes a double addition. A char in arithmetic is promoted toint first. You do not have to write a cast for those cases, but a cast makes the intent obvious.
| Expression | Result kind |
|---|---|
7 / 2 | int 3 |
(double)7 / 2 | double 3.5 |
7 / 2.0 | double 3.5 |
(int)3.9 | int 3 |
Casts are not magic
A cast does not change the original variable. (double)a produces a converted copy for that expression. a is still an int. Casting a pointer type is a different, sharper tool and is easy to get wrong; this chapter stays with numbers. Next: bitwise operators, which work on the bits inside those integers.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.
Physics
Milliamps to amps
20 mA is 0.020 A. Integer 20 / 1000 is 0 in C. Dividing by 1000.0 promotes the expression. Circuit formulas want amps. Milli- on a meter is a display choice.
I(A) = I(mA) / 1000
Example
#include <stdio.h>
int main(void) {
int milliamps = 20;
double amps = milliamps / 1000.0;
printf("%.3f A\n", amps);
return 0;
}Maths
Mean without truncation
8, 11, and 5 sum to 24. 24 / 3 is 8 as int or as a real. Change the list to 8, 11, 6 and the integer mean becomes 8 while the real mean is 8.33. That is why the listing prints both.
Example
#include <stdio.h>
int main(void) {
int sum = 8 + 11 + 5;
printf("truncated = %d\n", sum / 3);
printf("real = %.2f\n", sum / 3.0);
return 0;
}