Programming Fundamentals

Variables & Data Types

12 min · 2 worked examples · 3 questionsFree

What a variable really is, and why choosing the right type matters on a microcontroller.

After this lesson you can

  • Choose the right type for a value
  • Predict when an int will overflow
  • Say why you never compare two floats with ==

A variable is a named box in memory

When you write int sensorValue = 0; you are asking the compiler to set aside a small region of memory, label it sensorValue, and put 0 in it. From then on, using the name reads or writes that region.

The crucial idea is that the name is for you, not the machine. The processor only knows about addresses. Good variable names are therefore a message to the next human who reads the code, including you in six months. sensorValue tells a story; x does not.

In the full lesson

  1. A variable is a named box in memory
  2. Types decide size and meaning
  3. Overflow: when numbers wrap around
  4. Floats are approximations

Free with an account. Read it, work the examples, answer the questions, then build it in the simulator.