Fundamental to programming is the concept of a variable, a piece of data that can change as the program runs. A variable could be an integer such as a page number in a book, a character such as the first letter on the page, a floating point (decimal) number such as the height of that character in centimetres, or many other things, but the important similarity between them is that they can all change.

C++ is a statically-typed language, meaning that a variable can only store a certain kind of data—such as an integer, character, or floating point number—and that type must be declared when the variable is introduced. Whilst the contents of a variable can change, it’s type cannot. The common built in types in C++ (those available without including any parts of the standard library) are

• int - Stores integers such as 4 or -3900. Is at least 16 bits long, but usually 32.
• char - Stores a single character such as 'c'. Is at least 8 bits long.
• float - Stores floating point values such as 3.1555. Is usually 32 bits long.
• double - Similar to float but with twice the precision. Is usually 64 bits long.
• bool - Stores true/false values.
• void - Cannot store data directly, but acts a bit like a placeholder.

### Usage

Before a variable can be used to store any data it must be declared by first specifying the type, then the variable name (which must start with a letter but can then contain any alphanumeric character or underscores, and can be of any length), and then ending the line with a ;. A variable declaration is an example of a statement, and all statements in C++ must end with a ;.

We can then assign values to the variables and perform the usual arithmetic operations, though the relevance and availability depends on the type (you can’t do much to a bool).

At the end of that foo will have the value 66, bar will have the value 10, and baz will equal 54.76. Note that unlike in mathematics where = denotes equality, in C++ = is the assignment operator and will change the value of the variable on the left hand side to the result of the expression on the right hand side.

It’s cumbersome to split an assignment and a declaration into two lines, so C++ allows you to combine the two into an initialisation, as well as combine multiple initialisations together.

If a variable is declared but not assigned a value it will often be zero, but don’t count on it, always initialise! (Don’t do what I did in the first example.)

The operations valid on all numerical types (including char, which stores characters as numbers as described ASCII/UTF-8) are +, -, *, and / as you would expect. One caveat with / however is that because int can only hold integers, / will truncate decimals and only leave the integer part. Unlike some programming languages, there is no exponentiation operator (^ exists but it’s unrelated). Specific to integer types is the modulo operator %, which returns the remainder in a division.

There are also additional unary operators ++ and -- (the increment and decrement operators) which only take one operand instead of two. The first increases the value of a variable by 1, and the second decreases the value by 1. They can be placed both before and after a variable, but their behaviour is subtly different in each case.

It’s good style to stick to either post-incrementing (foo++) or pre-incrementing (++foo) when the order doesn’t matter and only use the other when the order is relevant. I prefer to pre-increment, because the computer can do it faster than post-incrementing.

Additionally the assignment operator can be combined with any binary (acts on two variables) operator discussed above as a shorthand.

This also applies to several other operators that we haven’t discussed, not just the arithmetic ones.

### Modifiers

C++ provides a selection of modifiers that can be applied to variable types when they are declared, creating a much larger list of variable types. The main modifiers are signed, unsigned, short, long, and long long. signed and unsigned specify—as they imply—whether or not the sign of an int or char should be taken into account. Since it takes 1 bit to store the sign, by gaining the ability to use negative numbers the maximum value of the variable is halved.

When declaring an int the signed is implied, but when declaring a char you should be explicit as it’s left up to the compiler whether it is signed or unsigned. If you’re dealing with actual characters then you should use char without any modifiers, but if you just want to store small numbers then specify either signed char or unsigned char to be safe.

short, long, and long long alter then number of bits used for the variable and hence affect the range of numbers that can be stored. These can only be applied to int, and can be combined with signed and unsigned.

Finally, long can be applied to the double type to increase the precision even further, but it cannot be applied to float (wouldn’t a long float just be a double?) and the standard double type is sufficient in most situations.

There is also an additional modifier, const, which declares a variable to be constant. This goes against the concept of a variable somewhat, because by declaring a variable as const you tell the compiler that its value will never be changed and it will instead keep the value it was initialised with. If you try to change a const variable, the compiler will error.

The last modifier we will cover is auto, which instructs the compiler to deduce the type of a variable from the value it is being initialised with; you replace int, double etc. with auto and let the compiler figure things out. It’s good practice to explicitly declare types if you can but it’s a very handy feature when dealing with long type names which we’ll come across later.

### Type Conversion

Ordinarily two types are incompatible with each other, so you can’t assign a char value to an int variable, however C++ automatically converts between types to make this possible. Type promotion occurs when a value with a small type is assigned to a variable with a larger type, e.g. signed char and signed short are promoted to int, and floats are promoted to doubles. This means the following is valid even though the types don’t agree.

When promotion cannot occur, a type conversion might. These are pretty logical and work how you would expect; floats are rounded down when assigned to ints, for example.

You can also explicitly convert types by using type casting, which has two equivalent forms.

### Arrays

If we want to store multiple related values, it’s annoying having to declare each one individually. We can avoid this problem by defining an array, which is a sequential list of variables of the same type. An array can be made from any type (not just the built in ones we’ve seen so far). Each individual variable can be accessed by its index, or position in the array.

[] in C++ are reserved for array element access and creation; in declarations they specify the number of elements in the array, and in expressions they access the given element. Note that array indices in C++ are zero-based—the first element is at position 0, not 1 (unlike some other languages). This may seem counter intuitive at first, but it’s vastly more useful! (Though I admit it has led me to sometimes start counting from zero, with embarassing results.) We won’t see the use until later when we cover loops and pointers though, so you’ll just have to trust me.

Instead of assigning values to each element of the array individually, we can initialise the array with a set of values using an initialiser list specified by {}. There does not need to be the same number of elements in the array as the list (though there must be a greater or equal number), although elements not given a value will be uninitialised. Additionally when initialising an array using {}, you can omit the size of the array and let the compiler work it out for you.

If you are only interested in initialising specific elements in an array, you can pass individual element indices to the initialisation list. The behaviour is best illustrated with an example.

### References

Consider a situation where we have an array of values and want to use one of them in a later piece of code. What’s more, we want to be able to modify that value and see the changes reflected in the array. We can’t just set a variable to the required value, because changing that variable won’t affect the value in the array; they’re completely separate things that just happen to have the same value. One solution is to carry around the index of the element, but then we have to keep on accessing the array.

C++ offers an alternate solution by way of references. A reference provides an alternate label for a variable, such as an element in an array. References are treated a lot like actual variables, but anything we do to the reference happens to the original variable too.

References are purely labels for existing things, they aren’t guaranteed to even exist in the compiled code because the compiler can optimise them out and replace them with the variable they refer to. This means that you can’t have arrays of references, or references to references.

### Basic Input and Output

Before we proceed it makes sense to briefly describe how to input and output data. This is quite an in depth topic but since it’s so crucial we’ll give it a short bit of attention.

Input and ouput in C++ works through the use of streams, which are essentially sources and destinations for data. Input streams accept data from a file or the user, and output streams send data to other files or back to the user. If you want to use input and output then don’t forget to include the iostream header as discussed previously.

cin is the standard input stream between the keyboard and the program, and cout is the standard output stream between the program and the command prompt/terminal window. The std:: in front denotes them to be part of the C++ standard library, and is required in front of anything contained in included standard library headers such as iostream or cstdlib.

The >> and << are stream operators, which point in the direction the data flows in—from the input to the output—and must be used with their respective stream.

Note that std::cin will pause the program and wait until the user presses Enter before continuing. It will also only read up to the first piece of whitespace, so if the user enters 33 54 the program will only see the 33. This pausing can be a handy property as it allows you to keep the program open if it closes immediately (this happens if you just run the program instead of opening it from a command prompt/terminal), although a more streamlined way is to use

which will also pause the program but does not need any variable unlike std::cin >> .

The >> and << operators can also be chained together in order to process multiple input or output statements in a row, and is very useful to split up different types which cannot be combined in the same statement. The example above first outputs a string of characters, then outputs an int, and finally prints a single character given by std::endl, which tells C++ to output the newline character '\n' (same as pressing Enter) and display the text on the screen.

### Exercise

Hopefully now you’ve got a decent grasp of variables and how to manipulate them, but pratice is important so as a brief exercise you should write a program that retrieves three floating point values from the standard input and then prints their average.

Solution