Programming & Scripting Tutorials

C++: Variables and Constants



We have gone over variables before – but because they are such an important concept in C++, I thought I’d make a lesson dedicated to variables and constants.
A variable is like a box that can only hold a certain type of value – for example a whole number in the case of the ‘int’ data-type. We can easily see what is currently inside the box, and we can easily change what’s inside the box.
A constant is very simply a value that doesn’t change – for example: 3.

We can declare a variable (create the box) by simply typing the data-type followed by the variable name:

datatype VariableName;


We can then initialise the variable (give it a value for the first time) either at declaration or later on. If we want to initialise it at declaration, we can simply put an equals sign followed by the value we want to assign it:

datatype VariableName = value;


If we want to initialise a variable at some other point in the program (or we can also use this to set it to a value at any point in the program), then we can just specify the variable’s name and then set it equal to something:

datatype VariableName;
VariableName = value;


We can also use operators such as ‘+=’ and ‘-=’ to add or subtract values from a variable easily. So if we wanted to subtract 5 from a variable called ‘age’, we could do the following:

age -= 5;


It’s also worth noting that you can easily declare many variables of the same type easily by separating them with a comma:

int age, height, RandomInteger;


Since variables and constants in C++ are declared differently depending on their data-types – the rest of this tutorial will go through some of the most common data-types.

Data-types usually come in two kinds – signed and unsigned. If you don’t specify when creating a variable if you want it signed or unsigned, the compiler will use it’s default (which is usually what you want for the most part (and is usually signed)). Signed means that you can set it to negative and positive numbers, and unsigned means that you can only set it to positive numbers (but higher positive numbers).


The integer is one we’ve covered already. You can declare an integer (whole number) using the ‘int’ data-type in C++, and can write integer constants by simply writing numbers. Signed integers on a 32bit computer can go from around -2147483647to around 2147483647.
An example of integer variables and constants:

int IntegerVariable; //Declare IntegerVariable as an integer variable
IntegerVariable = 5; //Initialise IntegerVariable to the constant of ‘5’


Float is a data-type that is a "floating point number". It’s for decimal values, and is accurate to about 7 digits. Float constants are defined by simply writing the number with decimal points. For example:

float FloatVariable; //Declare FloatVariable as an floating point variable
FloatVariable = 3.141592; //Initialise FloatVariable to the constant of ‘3.141592’


Doubles are very similar to floats, but can handle more digits (they are accurate to about 15 digits). Double constants are defined as most number constants are, by simply writing the number. For example:

double DoubleVariable; //Declare DoubleVariable as a double precision floating point variable
DoubleVariable = 3.1415929999; //Initialise DoubleVariable to the constant of ‘3.1415929999’


While we’re doing all these number data-types, let’s also go over ‘long’ and ‘short’. Signed longs on a 32bit computer can hold long numbers from around -2147483647 to around 2147483647 (notice that this is actually the same as the int range – longs and ints are pretty much the same on most systems these days). Shorts can hold short numbers – signed shorts on a 32bit computer can hold numbers from around -32767 to around 32767. Both constants are defined by simply writing the numbers.
An example of these two is as follows:

short LongVariable; //Declare LongVariable as a long variable
LongVariable = 1000000; //Initialise LongVariable to the constant of ‘1000000’

short ShortVariable; //Declare ShortVariable as a short variable
ShortVariable = 10; //Initialise ShortVariable to the constant of ‘10’


Ok, so enough of number data-types already! Let’s move onto some other kinds.
Chars are values that hold single characters (or in fact can hold very small numbers). They are one of the few data-types that usually default to unsigned – this is because chars usually want a value between 0 and 255. This is because chars use the ASCII system, which means characters are represented as numbers from 0-255. Char constants are defined either with single quotes, like so: ‘a’, or as the actual ASCII numbers, like so: 97.
For example:

char CharVariable; //Declare CharVariable as a char variable
CharVariable = 'a'; //Initialise CharVariable to the constant of ‘a’


Another interesting data-type is the boolean. This can hold a value of ‘true’ or ‘false’ (or ‘1’ (true) or ‘0’ (false)). Boolean constants are defined by writing the keywords ‘true’ or ‘false’ (or using ‘1’ or ‘0’). For example:

bool BoolVariable; //Declare BoolVariable as a bool variable
BoolVariable = true; //Initialise BoolVariable to the constant of ‘true’


And finally, the last (and possible most complicated) data-type that we will be looking over this lesson, is the string. Strings are a combination of characters (usually up to 4294967291 characters) however they need another include to use – string. Once this has been included, you can just create strings like any other data-type.
String constants are defined with double quotes. For example:

#include <string>
//Other pieces of program structure go here (namespace, main function etc.)
string StringVariable; //Declare StringVariable as a string variable
StringVariable = "Hello!"; //Initialise StringVariable to the constant of "Hello!"


This C++ tutorial was written by


Back to C++

Advertisement: