FUNDAMENTOS
Variables: what they are and how they work, revisiting the 2024 post
In 2024 I explained variables with the image of a box in memory and the ASCII table. Revisiting it, two things need adjusting: Python doesn't work exactly like a box, and the example itself had text outside ASCII.
In February 2024 I wrote here about variables. It was the subject the 2017 intro-to-programming series left on hold after #2 - What is an algorithm?. The text started from an example in the book by Ana Fernanda Gomes Ascencio and Edilene Campos, which I keep. Revisiting it, I split the explanation differently and corrected two points.
Two people adding
The book’s example is a conversation. One person suggests adding two numbers and says the first: 5. The other keeps that number in memory. Then comes the second: 3. They keep it too, in a different place, without erasing the first. When the first person asks for the result, the second retrieves both values, does the math and answers 8.
The computer does the same. A program receives data that has to be kept until it’s used, and the place for that is memory. A variable is a memory location with a name and a type, whose content can change while the program runs. It can take different values over time, but holds one at a time.
first = 5
second = 3
result = first + second
print(result) # 8
How the value ends up in memory
The computer works in binary. Each digit, 0 or 1, is a bit; eight bits make a byte; and each byte of memory has an address.
Text also becomes a number before becoming binary. In the 2024 post I used the ASCII table, in which each character has a code:
| Character | Decimal | Binary |
|---|---|---|
| A | 65 | 01000001 |
| B | 66 | 01000010 |
| C | 67 | 01000011 |
| F | 70 | 01000110 |
To grasp the idea, the table works. What I didn’t say is that ASCII has only 128 characters, and none of them is an accented letter. The post itself had a shopping list with “maçã” (Portuguese for apple), which doesn’t fit in ASCII. Today text is almost always stored as UTF-8, which keeps the ASCII codes for unaccented letters and uses more than one byte for the rest. You can see it in Python:
for letra in ["c", "ç", "变"]:
bytes_utf8 = letra.encode("utf-8")
print(letra, ord(letra), bytes_utf8, len(bytes_utf8))
# c 99 b'c' 1
# ç 231 b'\xc3\xa7' 2
# 变 21464 b'\xe5\x8f\x98' 3
Each line shows the letter, its code (ord), the bytes stored (encode) and how many bytes there are (len). To
read the bytes: in b'\xc3\xa7', each \x followed by two hexadecimal digits is one byte, so there are two, c3 and
a7. In b'c' Python shows the character itself, because the byte is the ASCII letter “c”: a single byte.
The number of bytes comes from the character’s code. UTF-8 uses 1 byte for codes 0 to 127, which are exactly the ASCII ones; 2 bytes from 128 to 2047; 3 bytes up to 65535; and 4 bytes above that, where emoji live. “c” has code 99, so it takes 1 byte. “ç” has 231, which is above 127: 2 bytes. “变”, the first character of “variable” in Chinese, has 21464: 3 bytes.
The name and the address
In the 2024 text, following the book, I wrote that every computer has an allocation table with the name, type and address of each variable. As a model, it helps: the programmer uses the name and something translates it into an address. But that something isn’t the computer itself. What keeps this mapping is the language’s compiler or interpreter. In a compiled C program, for example, variable names don’t even exist anymore when the program runs; only the addresses remain.
The box and the label
I also used the image of a box: the variable age would be a box where you put the number 25. For languages like C,
the image is good, because the variable really is a piece of memory whose size is set by the type.
The post’s examples, however, were in Python, and in Python the image that works better is the label. The value lives in an object in memory, and the variable is a name stuck on it. Two names can be stuck on the same object:
shopping = ["apple", "banana", "milk"]
items = shopping
items.append("bread")
print(shopping) # ['apple', 'banana', 'milk', 'bread']
Anyone thinking in boxes expects shopping to keep three items, as if items were a copy. It isn’t: both variables
point to the same list. For a real copy, you have to ask for one: items = shopping.copy().
What remains
A variable is a name for a value kept in memory. The rest, like the type, the size and what happens when two variables point to the same value, depends on the language, and it’s worth checking in whatever language you’re using.
Source: ASCENCIO, Ana Fernanda Gomes; CAMPOS, Edilene Aparecida Veneruchi de. Fundamentos da Programação de Computadores. 3rd ed. São Paulo: Pearson, 2012. p. 22.
Who writes this
Eduardo Lira
Software developer in Rio de Janeiro. I started this blog in 2012, in technical support at a hosting company, writing down the fixes for the problems that came up every day. Today I work at Sofis Tecnologia, on critical healthcare systems and SQL Server database migrations.