Variables

Published May 6, 2021Updated Dec 21, 2022
Contribute to Docs

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, the type must be specified and a value must be assigned to it:

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

Code
Output
Loading...

All contributors

Looking to contribute?

Learn C++ on Codecademy