Remember Values
Explore how to create and use variables in C++ to store values like numbers and characters. Learn to declare variables, understand key data types, and update stored information in your programs.
We'll cover the following...
You now know how to print to the screen and perform basic math with C++. Now, let’s give your program a memory. In this lesson, you’ll learn how to create variables, containers for numbers, letters, or anything your program needs to remember.
Goal
You’ll aim to:
Declare variables using C++ types.
Store and reuse values.
Understand type basics:
int,double,char.
Make a variable
To declare a variable in C++, we use the following simple syntax:
Now, let’s use this syntax in the code below to declare and use a variable of integer (int) data type:
Here is a visual representation of our variable, score:
More types
Here are some additional data types we can use for our variables, depending on the type of value they will hold:
You’ve used three core types.
Store the result in a variable
We have already done some math with C++. Now, let’s store the result of our calculation in a variable:
Now your math lives inside a named value.
Change a variable
We don’t just declare a variable and use it unchanged throughout our program; the value a variable holds can be changed or updated (hence the name ‘variable’):
You have updated and reused a variable.
Quick type reference
int→ Whole numbersdouble→ Decimals/floating pointchar→ Single letters or characters