Variables and declarations
A program that can’t remember anything can’t do much. It will compute a result and immediately throw it away. Variables are how you give a value a name so you can use it later. That’s the whole idea. Everything else in this lesson is just the mechanics of making that work in C++.
What a variable actually is
When your program runs, the operating system gives it a chunk of memory to work with. Think of that memory as a long row of boxes. Each box can hold a small piece of data.
A variable is your way of saying: “reserve one of those boxes for me, and let me refer to it by a name.” The name is just a convenient label you can use to retrieve the value in that box.
Declaring a variable
In C++, before you can use a variable, you have to declare it. A declaration tells the compiler two things: what the variable is called, and what kind of data it will hold. For example:
int score;Here, int is the type of the variable, meaning the variable can hold a value which is an integer (a whole number). score is the name you chose. The semicolon ends the statement, as required by C++.
After this line, the compiler has reserved space for one integer and agreed to call it score. Until you store a value in it yourself, the variable contains an unknown value, also called a garbage value.
Initializing a variable
The solution is to initialize the variable at the same time you declare it:
int score = 0;Now score exists and its value is 0. This is called initialization because you’re setting the initial value.
C++ also supports initialization using curly braces:
int score{0};This has the same effect as int score = 0;.
Here’s a complete program:
\n moves the cursor to the next line. It’s a simpler alternative to std::endl, which you saw in a previous lesson.
Assigning a value later
Initialization sets a value once, at declaration. Assignment changes a value after the fact.
The = operator takes the value on the right and stores it in the variable on the left. The statement score = 10 can be read as "score is assigned 10."
Here’s a program that demonstrates assignment:
You should see the output:
Score: 0
Score: 10
Score: 25The variable holds one value at a time. Each assignment replaces whatever was there before.
Naming variables
The language has rules for naming variables, and these are enforced by the compiler. There are also conventions followed by the C++ community.
The rules:
- Names can only contain letters, digits, and underscores, but cannot start with a digit.
2scoreis not a valid name. - C++ is case-sensitive, meaning
score,Score, andSCOREare three completely different variables. - Names cannot be reserved keywords: words the language has claimed for itself, like
intorreturn.One more thing worth knowing: names starting with underscores are often used by the compiler and standard library internals, so avoiding them prevents accidental clashes.
The conventions:
C++ codebases tend to use one of two styles for multi-word variable names:
camelCase: first word is lowercase, each following word is capitalized. For example,playerScoreandmaxRetries.snake_case: all letters are lowercase, words are separated by underscores. For example,player_scoreandmax_retries.
Photo credits: https://www.svgrepo.com/
Neither is wrong, but consistency within a project does matter. Many modern C++ style guides lean toward snake_case for variables, though.
The goal of a name is to make the code readable. So score is better than s, and account_balance is better than ab.
Declaring multiple variables
You can declare several variables of the same type on one line:
int width = 800, height = 600;This is valid C++, but one variable per line is easier to read and is the more common practice.