C++ Variables

A variable is a name given to a particular area in memory that stores a value, which can later be reused. When creating a variable in C++ it is necessary to specify the type of data to be stored within the variable, for example, whether it is a character, string, integer or floating-point number. The value of a variable can be changed as much as is necessary once it has been created.

When naming variables in C++ it should be noted that the names are case sensitive. Variable names must start with a letter or underscore, but can then be followed by either letters, numbers or underscores and should not exceed 31 characters in length. This limit of 31 characters is to make the variable names portable across different environments. It is actually possible to go up to 255 characters in some circumstances. One further restriction is that a variable name cannot be the same as a reserved word. Reserved words, or keywords, as they are sometimes referred to, are those that have a specific purpose in C++ and therefore cannot be used for anything else.

When declaring a variable, it takes the following format.

variable_type variable_name;

Variable name is the name given to the variable in memory, which can be used to reference it later. Variable type refers to the type of data that can be stored in a variable. The most commonly used data types in C++ are ‘char’, ‘string’, ‘bool’, ‘int’, ‘float’ and ‘double’, where ‘char’ contains a single character, ‘string’ comprises of a string of characters, ‘bool’, short for Boolean, is either ‘true’ or ‘false’, represented by 1 for true and 0 for false, ‘int’ is a whole number, ‘float’ refers to a number that can contain decimal places, such as 1.5, and ‘double’, which is similar to ‘float’ but can contain numbers with greater precision.

Below are examples of variable declarations.

char firstInitial;
string fullName;
bool result;
int age;
float price;
double total;

Once a variable has been declared, a value can be assigned to it.

firstInitial = 'F';
fullName = "Fred Bloggs";
result = 1;
age = 30;
price = 12.99f;
total = 12.999999;

One thing to note when initialising character and string variables is the use of quotes. For character variables, single quotes must be used, whereas, with string variables, double quotes are utilised.

Instead of declaring a variable and then assigning a value to it on a separate line, the two operations can be combined, so the above can be re-written as follows.

char firstInitial = 'F';
string fullName = "Fred Bloggs";
bool result = 1;
int age = 30;
float price = 12.99f;
double total = 12.999999;

If more than one variable of the same type needs to be declared and initialised, it is possible to do this in one line of code, with the variables separated by commas.

char firstInitial = 'F', lastInitial = 'B';

There are also two other methods of initialising variables in C++. the first uses constructor initialisation syntax, whilst the second utilises list initialisation syntax. Examples for both can be found below.

// Constructor initialisation syntax.
char firstInitial ('F');
string fullName ("Fred Bloggs");
bool result (1);
int age (30);
float price (12.99f);
double total (12.999999);

// List initialisation syntax.
char firstInitial {'F'};
string fullName {"Fred Bloggs"};
bool result {1};
int age {30};
float price {12.99f};
double total {12.999999};

The examples shown here are just a small number of the types of variables that are available.

Variable Declaration with the 'auto' Keyword

All of the above examples explicitly specify the type of the variable, on the left hand side prior to the name. The 'auto' keyword can be used to implicitly specify the variable type, based on the value being assigned. It is left to the compiler to decide the type for the variable.

Here are the above examples declared using the 'auto' keyword.

auto firstInitial = 'F';
auto fullName = "Fred Bloggs";
auto result = 1;
auto age = 30;
auto price = 12.99f;
auto total = 12.999999;

It should be noted that the 'auto' keyword can only be used when the variable is declared and initialised at the same time and cannot be used in every situation. A good use case is where the type of the value being assigned is not clear. The 'auto' keyword also works with constructor and list initialisation syntax.