Primitive Types and Data in Memory
A variable is not a box with a name. It is an address and a set of rules for reading what is there.
A primitive type is a fixed-size pattern of bits with an agreed meaning — which means the same four bytes can be an integer, a float, or garbage, depending only on the rule you read them by. Every structure in this course is built on that bargain: know the size, know the reading rule.
Types Are Sizes and Rules
A primitive type is a fixed-size pattern of bits with an agreed meaning. An integer, a floating-point number, a character and a boolean each occupy a known number of bytes, and the operations on them are defined by the type, not by the value.
Four facts follow, and every one of them causes bugs when forgotten.
Size is fixed by the type. An int in a particular language and machine is 4 bytes; a char is 1. The
size does not grow to fit your number.
Range is fixed by the size. A 32-bit signed integer holds −2,147,483,648 to 2,147,483,647. Add one to the largest and it wraps to the smallest — the overflow that has crashed spacecraft and payment systems.
Integers and floating-point numbers are different kinds of thing. Integers are exact; floating-point
values are binary fractions, so 0.1 is not exactly representable, and 0.1 + 0.2 is not 0.3. Money in a
float is a bug waiting for an audit.
Characters are numbers. A character is a code point in a character set — ASCII for the English letters,
Unicode for the world. 'A' is 65, and 'A' + 1 is 'B', which is why string comparison is numeric
comparison underneath.
Choosing a type is choosing a size, a range and a set of rules. There is no neutral choice.
Sizes, Ranges and the Costs of Getting Them Wrong
Sizes in one common model: char 1 byte, short 2, int 4, long long 8, float 4, double 8,
bool usually 1. A pointer is the size of an address on that machine: 8 bytes on a 64-bit system.
The practical failures:
- Overflow: an
intcounter in a system handling billions of transactions rolls over to a negative number. Use a wider type, or a checked type. - Truncation: assigning a
doubleto anintsilently drops the fraction. 4.99 becomes 4. - Precision: storing money as
floataccumulates errors; store the smallest unit as an integer, or use a decimal type. - Character set assumptions:
charin C is one byte, so it cannot hold a Unicode code point above 255. Text handling needs an explicit encoding decision.
None of these is a language flaw. Each is the consequence of a fixed representation being asked to do something it was not sized for.
Pause & think
Your friend's payment app computes 0.1 + 0.2 == 0.3 and finds it false. The two sides print as the same
number, though. What is happening, and what is the safe fix?
📌 If you remember nothing else…
- A type fixes a size, a range and a set of operations; variables are addresses plus those rules.
- A 32-bit signed integer overflows at 2,147,483,647; floating point is inexact by construction.
- Truncation and precision loss happen silently, which is what makes them expensive.
- Characters are numbers in an encoding; text handling needs an explicit choice of encoding.
Quick check
Did it land?
Spotted a mistake?