...

/

Remember Values

Remember Values

Declare and use int, double, and char variables.

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:

Press + to interact
Variable declaration in C++
Variable declaration in C++

Now, let’s use this syntax in the code below to declare and use a variable of integer (int) data type:

Press + to interact
#include <iostream>
using namespace std;
int main() {
int score = 100; // Stores 100 in 'score'
cout << "Score: " << score << endl; // Prints the value of score (100) with label (Score: )
return 0;
}

Here is a visual representation of our variable, score:

Press + to interact
Breakdown of a variable declaration in C++
Breakdown of a variable declaration in C++

More types

Here are some additional data types we can use for our variables, depending on the type of value they will hold:

Press + to interact
#include <iostream>
using namespace std;
int main() {
double price = 4.99; // Decimal number
char grade = 'A'; // Single character
cout << "Price: $" << price << endl;
cout << "Grade: " << grade << endl;
return 0;
}

You’ve used three core types.

Press + to interact
int, double, and char
int, double, and char

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:

Press + to interact
#include <iostream>
using namespace std;
int main() {
int total = 8 + 12; // Creates an integer variable named total and stores the result of 8 + 12 in it
cout << "Total: " << total << endl; // Prints the text "Total: " followed by the value of total, then moves to a new line
return 0;
}

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’):

Press + to interact
#include <iostream>
using namespace std;
int main() {
int age = 18; // Stores 18 in age
age = age + 1; // Increases age by 1
cout << "Next year: " << age << endl; // Prints updated age with label
return 0;
}
Press + to interact
Updating a variable in C++
Updating a variable in C++

You have updated and reused a variable.

Quick type reference

  • int → whole numbers

  • double → decimals/floating point

  • char → single letters or characters

Mini challenge

Create a small program with the following:

  • Your name (each alphabet as char)

  • Your age (as int)

  • Your height (as double)

  • Print a sentence using all three.

Press + to interact
#include <iostream>
using namespace std;
int main() {
// Your code goes here
return 0;
}

If you’re stuck, click the “Show Solution” button.

We declared individual char variables for each letter of the name. Now, imagine a name that’s 15 characters long—or a sentence with over 100 characters. Would we declare 100+ char variables in that case?

What’s next?

You’ve learned to remember numbers. Now let’s level up and work with words using strings!