A variable refers to a storage location in the computer’s memory that one can set aside to save, retrieve, and manipulate data.

Declare a Variable

To create a variable, you must specify the type and assign it a value:

type name = value;

So to create a variable called score of type int and assign it the value 0:

int score = 0;

Display Variables

The std::cout object is used together with the << operator to display variables.

To combine both text and a variable, separate them with the << operator:

int myAge = 30;
std::cout << "I am " << myAge << " years old.";

The output would be:

I am 30 years old.

Codebyte Example



Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn C++ on Codecademy