JavaScript Error

You currently have JavaScript disabled on your web browser.

This website uses JavaScript, and This web page needs JavaScript activated to work correctly.

Please active JavaScript on your web browser and then refresh this web page.



Variables



Variables in C++ are containers used for storying data values of specific data types.

The following keywords are used to define different types of commonly used variables:


Declaring and Assigning Variables

To declare (i.e. create) a variable, the variable type must be specified when the variable name is defined. A value can be assigned to a variable either at the moment it was declared, or some time after it is declared:

 
// syntax format for declaring a variable and settings its value
type variableName = value;

// syntax format for declaring a variable without settings its value
type variableName;

With the Syntax formats shown above, substitute type with one of C++ variable type keywords (such as int), and variableName with the desired name of the variable itself (such as x or tempValue). The equal sign (=) is used to assign a value to the variable. Always close the instruction with the semicolon (;).

Best Practice: When naming your variables, it is best to stick to names that start with a letter, and steer clear of starting with double underscores (__) or underscored capitalized names (_CapitalizedName), since they often result in unexpected conflicts with reserved system variables.

Follow these rules when creating variable names:


The following is an example of how to create a variable that will be used to store a number:

 
// declare the variable and assign it a value
int myNum = 42;

// output the value contained within the variable
cout << myNum;

Or if you need or want to declare the variable first, and then assign it a value afterwards, you can do that as well:

 
// declare the variable
int myNum;

// assign it a value
myNum = 42;

// output the value contained within the variable
cout << myNum;

If you assign a new value to an existing variable, it will overwrite the previous value.

Note : Make certain that the new value is of the same variable type (i.e. int) as the variable is, otherwise bugs will be introduced and unexpected results may occur.

 
// declare the variable and assign it a value
int myNum = 42; // myNum = 42

// output the value contained within the variable
cout << myNum; // outputs 42

// assign the variable a new value
myNum = 1298; // myNum = 1298

// output the value contained within the variable
cout << myNum; // outputs 1298

Other Examples

The following is an example of how to declare variables of each of the different data types:

 
int myNum = 42;
float myFloat = 98.6;
double myDouble = 3.14159;
char myChar = 'A';
string myString = "Hello World";
bool byBool = false;

Display Value

To display the value of the variable, you can use the cout object together with the << operator. You can also combine both string text and a variable, separating them with the << operator, for example:

 
int myName = "Timmy";
int myAge = 53;
double myNetWorth = 1289.73;

cout << myName << " is " << myAge << " years old, and only has $" << myNetWorth << " in savings.";

Modifiers

Numerical data type variables can also have a modifier, to customize the base type:

 
unsigned int myScore = 100;
long long myBigNumber = 9000000000;

Derived Data Types

Data types can be derived and constructed from fundamental types:


User-Defined Data Types

C++ also allows you to create and develop your own structures for more advanced designs:

 
struct myStruct = { int x, y; };
enum myColors { RED, GREEN, BLUE };

Arithmetic Operations On Variables

To perform an arithmetic operation (i.e. add, subtract, multiply, divide, etc...) on and with variables, you can use the variable in place of the value:

 
int x = 3;
int y = 12;
int sum = x + y;

cout << sum;

Declare Multiple Variables

C++ allows you to declare more than one variable of the same data type, and assigning each their own value, by using a comma-separated list:

 
int x = 3, y = 74, z = 12;

cout << x + y + z;

You can also assign the same value to multiple variables of the same data type in one line:

 
int x, y, z;
x = y = z = 25;

cout << x + y + z;


Final Thoughts

Thank you for reading, I hope you found this blog post (tutorial) educational and helpful.


(0) pollYesResult
(0) pollNoResult



 
     About   |   Contact Us   |   Privacy   |   Terms & Conditions   |   © 2025 - T&J Divisions, LLC, All Rights Reserved