Interactive Computer Science Tutoring

Learn programming concepts interactively

AHA Schools Logo

Variables & Data Types in C

Learn how C stores data in memory with direct control over types and allocation

📚 Building on Hello World

What we learned previously:

Compilation Process: C transforms your source code into executable machine instructions.
Today's Goal: Learn how C stores different types of data in memory with precise control.
🤔 Memory Prediction Challenge

Before exploring variables, predict: If you declare int age = 25, what do you think happens in computer memory?

🏘️ Welcome to Memory City

Think of computer memory as a neighborhood where each variable lives in its own house. Every house has a unique address (like 0x7fff1000) so the computer can find it instantly.

🏠 Houses (Variables)
Each variable gets its own house with a name and address
📍 Addresses
Like street addresses, each house has a unique memory location
📦 Different Sizes
Houses come in different sizes for different data types
Understanding C Variables

In C, every variable is a named memory location that stores data. Unlike higher-level languages, C requires you to explicitly declare the data type of each variable, which determines how much memory is allocated and how the data is interpreted.

Each variable has an address in memory (like a house address) and a value stored at that location. The type determines both the size and the interpretation of the stored bytes.

Memory Layout3 variables • 9 bytes total
Visual representation of your variables in memory
age
int4 bytes
25
Integer number (4 bytes)
height
float4 bytes
5.9
Floating-point number (4 bytes)
initial
char1 bytes
'J'
Single character (1 byte)
Create New Variable
Declare a new variable with a specific type and value
Update Variable Value
Modify the value of age
Quick Variable Creation
Variable Management
Key Concepts

🧠 Understanding Memory:

  • Each variable has an address: Like a house number where the value is stored
  • Type determines size: int (4 bytes), float (4 bytes), char (1 byte), etc.
  • Values are stored as binary: The type tells C how to interpret those bits
  • Stack allocation: Local variables are allocated on the stack automatically

⚠️ Important C Rules:

  • Must declare before use: C needs to know the type to allocate memory
  • Type cannot change: Once declared as int, it stays int (no dynamic typing)
  • Uninitialized variables: May contain garbage values from previous memory use
  • Case sensitive: 'age' and 'Age' are different variables
⚡ Memory Challenge

How much total memory do these three variables use: int age, float height, char initial?

📊 How Confident Do You Feel?
Not confidentVery confident3/5
Not confidentVery confident3/5
Not confidentVery confident3/5
Not confidentVery confident3/5
🏘️ Memory City Neighborhood

Variable Houses on Memory Street

Memory Street Layout
Cottage (1 byte)
House (4 bytes)
Mansion (8+ bytes)
age
int (4 bytes)
📍 0x7fff1000
Value: 25
height
float (4 bytes)
📍 0x7fff1004
Value: 5.9
initial
char (1 byte)
📍 0x7fff1008
Value: 'J'

🏘️ Each variable lives at a unique address in Memory City

🏠 House size depends on data type - bigger types need bigger houses!

C Variables Code
Interactive C code with memory visualization
History0/5
Variable creation and modification operations

No operations yet

Start by adding or modifying elements