Data Types
This study guide condenses Chapter 4 for developers coming from high-level, garbage-collected languages like JavaScript, Python or Go. In C, data types directly define memory layout, how bits are interpreted, and how fast the hardware can work with them.
1. Key concepts
Data types
A data type defines what kind of value a variable holds, how its bit pattern is read, and exactly how many bytes it occupies. Unlike JavaScript or Python, C is statically typed: every variable's type is declared before use. C groups its types into:
- Basic types: arithmetic types, meaning integers and floating-point numbers.
- Enumerated types:
enum, integer values with symbolic names. - The void type: no value. Used for return types, empty parameter lists and generic pointers.
- Derived types: pointers, arrays, structures, unions and functions.
The sizeof operator gives the storage size of a type or variable in bytes, as a value of type size_t, printed with %zu.
#include <stdio.h>
int main(void) {
int num = 10;
printf("int: %zu bytes\n", sizeof(int));
printf("num: %zu bytes\n", sizeof num);
printf("double: %zu bytes\n", sizeof(double));
return 0;
}Integers
Integer types store whole numbers. Their sizes depend on the platform; the standard only guarantees minimum ranges. On most 64-bit systems short is 2 bytes, int is 4 and long long is 8, while long is 8 bytes on Linux and macOS but 4 on Windows.
Float and double
float is a 4-byte single-precision IEEE 754 number with about 6 to 7 significant decimal digits. double uses 8 bytes and gives about 15 to 16. Floating-point literals such as 3.14 are double unless you add the f suffix.
Fixed-width integers
C99 added <stdint.h>, with exact-width types such as int32_t and uint8_t. Use them when the exact size matters: file formats, network protocols and hardware registers.
Glossary
| Term | Definition |
|---|---|
| Signed integer | An integer type that can hold negative and positive values. In two's complement the most significant bit carries the sign. |
| Unsigned integer | An integer type limited to zero and positive values, which doubles the positive range. |
| Single precision | The float format: 4 bytes (32 bits) with about 6 to 7 decimal digits of precision, per IEEE 754. |
sizeof | A unary operator that returns the size of a type or object in bytes, as a size_t. |
| Type qualifier | A keyword (const, volatile, restrict, _Atomic) that changes how an object may be accessed or optimised. |
| Two's complement | The representation of signed integers in which a negative value is formed by inverting all bits and adding 1. |