C Numbers
Numbers in C come in two broad families: integers for whole numbers and floating point types for numbers with a decimal part. Knowing the difference helps you pick the right type and avoid common surprises with division.
Integers
An integer, declared with int, holds a whole number with no decimal part. Integers can be positive, negative, or zero. They are the right choice for counting things.
Working with integers
#include <stdio.h>
int main() {
int a = 10;
int b = 3;
printf("Sum: %d\n", a + b);
return 0;
}Floating Point Numbers
For numbers with decimals, use float or double. A double is more precise and is the usual default choice for decimal work.
Working with decimals
#include <stdio.h>
int main() {
double price = 19.95;
double tax = 1.60;
printf("Total: %.2f\n", price + tax);
return 0;
}Number types compared
The Integer Division Trap
When you divide one integer by another, C throws away the fractional part and keeps only the whole number. So 7 / 2 gives 3, not 3.5. To get a decimal result, at least one value must be a floating point type.
Integer vs floating point division
#include <stdio.h>
int main() {
int intResult = 7 / 2;
double realResult = 7.0 / 2;
printf("Integer division: %d\n", intResult);
printf("Real division: %.1f\n", realResult);
return 0;
}