Fundamentals•Last Updated : 09 Oct 2026
~55 min read
Variables & Data Types
Comprehensive foundations on stack and heap allocation, 32/64-bit integer words, IEEE-754 floating point numbers, and type safety across compiled and dynamic languages.
1. The Computational Memory Model
When a program runs, every variable declared is allocated a specific contiguous block of bytes in Random Access Memory (RAM). The variable name acts as a human-friendly label for a physical memory address (pointer).
1a = 422b = a3print(f'a: {a}, memory id: {id(a)}')4print(f'b: {b}, memory id: {id(b)}')56# Reassignment creates a new immutable integer object7a += 18print(f'Updated a: {a}, new id: {id(a)}')9print(f'Unchanged b: {b}, id: {id(b)}')
Output
a: 42, memory id: 1407238202 b: 42, memory id: 1407238202 Updated a: 43, new id: 1407238234 Unchanged b: 42, id: 1407238202
2. Primitive Data Types & Bounds
Computers represent numbers using fixed-width binary words. Integers (32-bit: -2^31 to 2^31-1), Floating point numbers (IEEE 754 standard for decimals), Booleans (1 byte: true/false), and Characters (ASCII/UTF-8).
Integer Reassignment Variable Trace
Step 1 of 5
1
x = 102
y = 203
x = x + y4
y = x - y5
x = x - y| Step | x | y | State Evolution |
|---|---|---|---|
| 1 | 10 | undefined | x initialized to 10 |
Suggested Quiz
Question 1 of 1
What is the memory size and range of a signed 32-bit integer?
Quick Syntax & Reference Sheet
| Concept / Rule | Syntax / Usage |
|---|---|
| Integer Declaration | int x = 42; (C++) / x = 42 (Python) |
| Boolean Flag | bool active = true; (C++) / active = True (Python) |
| Memory Identity | &x (C++ address) / id(x) (Python object ID) |
Common Pitfalls & Developer Mistakes
✕ Mistake 1:
Integer Overflow
Why it happens: Adding numbers beyond 2^31-1 in C/C++ causes undefined behavior / rollover.
Correction: Use 64-bit integer types (long long in C++, int64 in Go/Rust).
Frequently Asked Questions
What is the difference between Stack and Heap memory?
Stack memory is fast, automatically managed LIFO storage for local variables and function frames. Heap memory is flexible, dynamically allocated memory for large or variable-size objects.
Hands-On Practice Problems
Practice Coding Challenges (2)Solve in Cloud IDE
Article Tags:#Variables#Memory#Primitives#Type Safety#Stack Allocation
Was this lesson clear and helpful?
Discussion (0)
Sign in to ask questions and reply.
Loading discussion…