Variables
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
All contributors
- garanews222 total contributions
- christian.dinh2476 total contributions
- Anonymous contributorAnonymous contributor3071 total contributions
- Christine_Yang271 total contributions
- garanews
- christian.dinh
- Anonymous contributor
- Christine_Yang
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.