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:
int
: integers (whole numbers), without decimals. For example: 1234 or -1234float
: single-precision decimal number. For example: 98.6fdouble
: double-precision decimal number number. For example: 3.14159 or -53.289334char
: single characters, such as 'a' or 'B'. When assigning the Char value to the Char variable, its value should be surrounded by single quotesstring
: text containing zero or more characters. For example, "Hello World". When assigning the String value to the String variable, its value should be surrounded by double quotesbool
: boolean value with two states: true or falseTo 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:
myVar
and myvar
are treated as two different variablesint
.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
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 ;
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." ;
Numerical data type variables can also have a modifier, to customize the base type:
short
, long
, long long
- used to vary the size of int
signed
, unsigned
- allow negative or only positive valuesunsigned int myScore = 100 ;
long long myBigNumber = 9000000000 ;
Data types can be derived and constructed from fundamental types:
float scores[5];
double calculate(int x, int y);
int* myPointer = &myAge;
int& myRef = myAge;
C++ also allows you to create and develop your own structures for more advanced designs:
struct
: Group related variablesclass
: Object-oriented encapsulationunion
: Share memory among different variablesenum
: Named constantstypedef / using
: Create aliases for typesstruct myStruct = { int x, y; };
enum myColors { RED, GREEN, BLUE };
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;
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;
Thank you for reading, I hope you found this blog post (tutorial) educational and helpful.
About | Contact Us | Privacy | Terms & Conditions | © 2025 - T&J Divisions, LLC, All Rights Reserved |