Every piece of data in C has a type. The type tells the compiler how much memory to reserve and how to interpret the bits. Choosing the right type is important โ use too little and you lose data, use too much and you waste memory.
Basic Data Types
C has five basic data types:
intโ whole numbers, like 42 or -7. Usually 4 bytes.floatโ decimal numbers, like 3.14. Usually 4 bytes.doubleโ double-precision decimal. More accurate than float. Usually 8 bytes.charโ a single character, like 'A' or 'z'. Usually 1 byte.voidโ no type. Used for functions that return nothing.
Size and Range
The exact size depends on your system, but here are the common values:
charโ 1 byte, range -128 to 127intโ 4 bytes, range about -2 billion to 2 billionfloatโ 4 bytes, about 6-7 decimal digits of precisiondoubleโ 8 bytes, about 15 decimal digits of precision
You can check sizes on your system using sizeof().
#include
int main() {
int a = 42;
float b = 3.14f;
double c = 3.1415926535;
char d = 'Z';
printf("int: %d\n", a);
printf("float: %f\n", b);
printf("double: %lf\n", c);
printf("char: %c\n", d);
return 0;
}
Try it Yourself โ