Labs ICT
โญ Pro Login

Data Types

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 127
  • int โ€” 4 bytes, range about -2 billion to 2 billion
  • float โ€” 4 bytes, about 6-7 decimal digits of precision
  • double โ€” 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 โ†’

๐Ÿงช Quick Quiz

Which data type stores a single character?