Tutorials Exercises Get Certified Services Search...
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
C++ Variables
❮ Previous Next ❯
C++ Variables
Variables are containers for storing data values.
In C++, there are different types of variables (defined with different keywords), for
example:
• int - stores integers (whole numbers), without decimals, such as 123 or -123
• double - stores floating point numbers, with decimals, such as 19.99 or -19.99
• char - stores single characters, such as 'a' or 'B'. Char values are surrounded
by single quotes
• string - stores text, such as "Hello World". String values are surrounded by
double quotes
• bool - stores values with two states: true or false
Declaring (Creating) Variables
To create a variable, specify the type and assign it a value:
Tutorials Exercises Get Certified Services
Syntax
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
type variableName = value;
Where type is one of C++ types (such as int ), and variableName is the name of the
variable (such as x or myName). The equal sign is used to assign values to the
variable.
To create a variable that should store a number, look at the following example:
Example
Create a variable called myNum of type int and assign it the value 15:
int myNum = 15;
cout << myNum;
Try it Yourself »
You can also declare a variable without assigning the value, and assign the value
later:
Example
int myNum;
myNum = 15;
cout << myNum;
Try it Yourself »
Note that if you assign a new value to an existing variable, it will overwrite the
previous value:
Tutorials Exercises Get Certified Services
Example
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
int myNum = 15; // myNum is 15
myNum = 10; // Now myNum is 10
cout << myNum; // Outputs 10
Try it Yourself »
Other Types
A demonstration of other data types:
Example
int myNum = 5; // Integer (whole number without decimals)
double myFloatNum = 5.99; // Floating point number (with decimals)
char myLetter = 'D'; // Character
string myText = "Hello"; // String (text)
bool myBoolean = true; // Boolean (true or false)
You will learn more about the individual types in the Data Types chapter.
Display Variables
The cout object is used together with the << operator to display variables.
To combine both text and a variable, separate them with the << operator:
Tutorials Exercises Get Certified Services
Example
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
int myAge = 35;
cout << "I am " << myAge << " years old.";
Try it Yourself »
Add Variables Together
To add a variable to another variable, you can use the + operator:
Example
int x = 5;
int y = 6;
int sum = x + y;
cout << sum;
Try it Yourself »
C++ Exercises
Test Yourself With Exercises
Exercise:
Create a variable named myNum and assign the value 50 to it.
Tutorials Exercises Get Certified Services
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
=
Submit Answer »
Start the Exercise
❮ Previous Log in to track progress Next ❯
COLOR PICKER
Tutorials Exercises
Services
Get Certified
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
SPACES UPGRADE NEWSLETTER
Top Tutorials
Tutorials
GET
Exercises Get Certified Services
HTMLCERTIFIED
Tutorial REPORT ERROR
HTML CSS CSS Tutorial
JAVASCRIPT SQL
JavaScript Tutorial FORUM PYTHON
ABOUT JAVA PHP HOW TO W3.CSS C
How To Tutorial
W3Schools
SQL Tutorial is optimized for learning and training. Examples might be simplified to
improve reading and learning.
Python Tutorial
Tutorials, references, and examples are constantly reviewed to avoid errors, but we
W3.CSS Tutorial
cannot
Bootstrap Tutorial full correctness
warrant
of
PHPallTutorial
content. While using W3Schools, you agree to have read and accepted our
terms
Java of use, cookie and privacy policy.
Tutorial
C++ Tutorial
Copyright 1999-2023 by Refsnes Data. All Rights Reserved. W3Schools is Powered by
jQuery Tutorial
W3.CSS.
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference
Top Examples Get Certified
HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate