Variables and declarations
Every useful program needs to remember things. 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.
int score;That’s called a declaration. 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 yourself, though, the box contains whatever happened to be there before you ran your program. That value is unpredictable, and using it will give your program unpredictable results.
Initialization: giving a value at birth
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 does the same thing. The curly-brace form has some advantages you’ll see later in the course, and many modern C++ codebases prefer it.
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.
Assignment: changing a value later
Initialization sets a value once, at declaration. Assignment changes a value after the fact. The operator is =.
It’s worth being clear about what = means in C++. It does not mean “these two things are equal.” It means “take the value on the right and store it in the variable on the left.” Reading it as “gets” instead of “equals” helps: score = 10 means “score gets 10.”
Here’s a program that demonstrates assignment:
You should see the output:
Score: 0Score: 10Score: 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. What matters is consistency within a project. Most 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.
What’s coming next
Right now you’ve only seen int as a variable type. C++ has a whole family of types based on the kind of values the variables can take. The next lesson explains what they are and when to reach for each one.