C ยท Chapter 8 of 45

C Data Types

C provides several built-in data types to store different kinds of values: int for whole numbers, float and double for decimals, char for single characters, and more. Choosing the right type affects both memory usage and precision.

The exact size of these types can vary by platform, but common sizes are 4 bytes for int, 4 bytes for float, 8 bytes for double, and 1 byte for char.

Syntax
int a;
float b;
double c;
char d;

Numeric types

int stores whole numbers, float stores single-precision decimals, and double stores double-precision decimals for greater accuracy. long and short modify the range of int.

Character and other types

char stores a single character (or small integer), and _Bool (or bool with stdbool.h) stores true/false values. The sizeof operator tells you the exact byte size on your system.

Example 1 (c)
#include <stdio.h>

int main() {
  int a = 5;
  float b = 5.5f;
  char c = 'A';
  printf("%d %.1f %c\n", a, b, c);
  return 0;
}
Output
5 5.5 A

Three different data types are declared, initialized and printed.

Example 2 (c)
#include <stdio.h>

int main() {
  printf("%lu\n", sizeof(int));
  return 0;
}
Output
4

sizeof(int) reports the number of bytes an int occupies, typically 4.

Key points

  • int, float, double and char are the core basic types.
  • double has more precision than float.
  • sizeof() reveals the byte size of a type on your platform.
  • Type sizes can vary slightly between compilers and platforms.
๐Ÿ’ก Note: Use double instead of float by default unless memory is extremely constrained, since it offers more precision.

๐Ÿ“ Quick Quiz

1. Which type is best for storing 3.14159?

2. Which operator tells you the byte size of a type?

3. Which type stores a single character?