A variable is an identifier for your data. It is a label for a memory location where data is stored. Data must live in computer memory to be manipulated, so every variable ultimately refers to an address.
Important
Variable names are not stored in computer memory. They exist only for programming convenience. The compiler replaces them with their corresponding memory location addresses when generating machine instructions. Post compilation, only the memory address exists, not the name.
Memory Organization
A typical computer memory is a sequence of numbered locations:
- Each location has an address (called a pointer in C).
- Each location holds a value (the data).
- A location in the example is 8 bits wide, so it can store one byte of data. To store or read data at that location, you use its address (e.g.
0x0803).
The width of a memory address depends on the computer architecture. It may be 32 bits, 16 bits, or 8 bits. On the ARM based microcontrollers covered later in this course, memory addresses are 32 bits.
How a Variable Fits In
Here is the whole idea in one picture. Suppose you write char valueA = 10; in your program. The diagram answers the three questions you would ask about any variable: which address is it, what does it store, and how big is it.
valueA (the name. It lives only in your source code.
The compiler throws it away and keeps the address.)
|
| wherever you wrote "valueA", the compiler
| puts the address 0x0800 instead
v
+---------+----------------------------------------------+
| address | 0x0800 |
+---------+----------------------------------------------+
| size | 1 byte = 8 bits (because the type is `char`) |
+---------+----------------------------------------------+
| stored | 0000 1010 = the value 10 in binary |
| value | |
+---------+----------------------------------------------+
Read the picture one line at a time:
- Which address is valueA’s? It is
0x0800. That single address is what the compiler kept after throwing away the name. - What is it storing? The value
10. Inside memory there are no digits, only bits, so the byte actually holds the pattern0000 1010. How to read bits as a number is explained in Binary Number System. - How big is it? One byte, 8 bits, because the type is
char. The computer’s memory as a whole is much bigger, with thousands of such addresses. ButvalueAowns only this one byte at0x0800, and nothing else.
And here is the same byte where it sits in memory, between bytes that belong to other data:
valueA ----+
| the byte from the picture above,
| now shown at its place among the others
v
address contents
+--------+ +-----------+
| 0x07FE | | 1010 1100 | a byte that belongs to something else
+--------+ +-----------+
--> | 0x0800 | | 0000 1010 | valueA's byte
+--------+ +-----------+
| 0x0801 | | 1111 0000 | a byte that belongs to something else
+--------+ +-----------+
... ...
(thousands more addresses above and below, one byte each)
Every row is one byte of memory. The arrow points at the only row that belongs to valueA. The bytes around it are not ours, they hold other data of the program.
So the name, the address, and the value are three different things. You write the name, the compiler turns it into the address, and the memory at that address holds the value.
Variable Definition
Before using a variable, you must define it. A definition tells the compiler you need memory space for your program data so it can reserve some. Depending on the data type:
char→ the compiler reserves 1 byteint→ the compiler reserves 4 bytes
The syntax states the data type, followed by a meaningful variable name:
char examScore;Here char is the data type and examScore is the variable name. Just by reading it, you can tell this variable stores an exam score.
Note
Variable definition is sometimes also called variable declaration. There is a subtle difference between the two, shown in Definition vs Declaration below. But people often use the terms interchangeably.
Variable Initialization
Initialization assigns a value to a defined variable:
char myExamScore; // definition
myExamScore = 10; // initializationWhen the compiler generates instructions for the initialization, it produces instructions that put the value into the memory location referred to by the variable name. Initialization must follow definition:
myExamScore = 10; // ILLEGAL: myExamScore is not defined yetThe compiler sees myExamScore being assigned before it is defined and throws an error. A variable must be defined before it is used for any data manipulation.
Definition and initialization can also be combined in a single statement, which is legal:
char myExamScore = 10; // definition + initializationRules for Naming a Variable
- Keep the name to no more than ~30 characters. Some compilers have issues with longer names.
- Only alphabets (uppercase, lowercase, or a combination), digits, and the underscore (
_) are allowed. No other special characters. - The first character cannot be a digit. It must be an alphabet or an underscore.
- Do not use C standard reserved keywords as variable names.
The reserved keywords are listed in the C99 standard, section 6.4.1. The full list (including C11 additions like _Alignas, _Alignof, _Atomic, _Generic, _Noreturn, _Static_assert, and _Thread_local) is documented at cppreference.
Legal and Illegal Examples
| Variable Name | Legal? | Why |
|---|---|---|
_hello | ✅ legal | starts with underscore, but not a meaningful name, so avoid this style |
1hello | ❌ illegal | starts with a digit |
hello1 | ✅ legal | digit after the first character is fine |
_hello_1 | ✅ legal | starts with underscore, valid characters |
hello_1 | ✅ legal | valid characters throughout |
$hello | ❌ illegal | starts with a special character; only underscore is allowed |
hello world | ❌ illegal | space is a special character, not allowed |
2helloworld | ❌ illegal | starts with a digit |
Definition vs Declaration
| Definition | Declaration | |
|---|---|---|
| Compiler action | Generates instructions to allocate storage for the variable | Is informed that a variable exists, along with its type |
| Storage allocated? | Yes | No |
Important
A variable definition is also a declaration, but not all declarations are definitions.
Previous: sizeof Operator
Related: Data Types And Variables, Char Data Type, Int And Long Data Types