Data Types, Sizes, and Limits

Chapter 6: Data Types, Sizes, and Limits

C is a statically typed language where every variable must have a type that dictates how many bits it occupies and how those bits are interpreted. Because C was designed for different architectures, the size of types can vary—a concept every system engineer must master.

I. Basic Data Types

C defines four primary categories of basic types:

TypePurposeCommon Size (64-bit)Format Specifier
charSingle character / Small integer1 byte (8 bits)%c or %d
intStandard integer4 bytes (32 bits)%d
floatSingle-precision floating point4 bytes (32 bits)%f
doubleDouble-precision floating point8 bytes (64 bits)%lf

II. Type Modifiers

Modifiers change the size or range of the basic types.

  1. signed vs unsigned:
    • signed (default): Half the range is negative, half is positive.
    • unsigned: Only zero and positive values. Doubles the positive range.
  2. short vs long:
    • short int: Typically 2 bytes.
    • long int: Typically 4 or 8 bytes.
    • long long int: Guaranteed at least 8 bytes (C99).

III. Fixed-Width Types (<stdint.h>)

Because int might be 16 bits on a microcontroller but 32 bits on a PC, portable code should use fixed-width types from stdint.h.

  • int8_t, uint8_t: 8-bit signed/unsigned.
  • int32_t, uint32_t: 32-bit signed/unsigned.
  • int64_t, uint64_t: 64-bit signed/unsigned.

IV. The sizeof Operator

sizeof is a compile-time operator that returns the size of a type or variable in bytes. It is essential for memory allocation.

#include <stdio.h>

int main() {
    printf("Size of char: %zu byte\n", sizeof(char));
    printf("Size of int: %zu bytes\n", sizeof(int));
    printf("Size of long long: %zu bytes\n", sizeof(long long));
    return 0;
}

V. Range and Limits (<limits.h> & <float.h>)

Every type has a maximum and minimum value it can hold. Exceeding these causes Integer Overflow, a common source of security vulnerabilities.

Signed 8-bit (char)-128 to +127Unsigned 8-bit (uint8_t)0 to 255

  • INT_MAX: 2,147,483,647
  • UINT_MAX: 4,294,967,295
  • CHAR_BIT: Number of bits in a char (usually 8).

VI. Boolean and Void

  • _Bool (C99): A type that holds only 0 (false) or 1 (true). Often used via <stdbool.h> as bool.
  • void: Represents the "absence of type." Used for functions that return nothing or for "generic pointers" (void *).