Lesson 4: Variables and Constants in C++

Understanding Variables and Memory in C++

Variables in C++ and indeed any programming language are just an easy way to store a value of some kind into the memory of the computer. All values are stored in a specific position in RAM (Random Access Memory) and each of these positions has a specific address assigned to it. It would be impossible for any programmer to remember all if not any of these memory addresses. This is why we name our variables. If you name a variable myName for instance, this now performs two actions, its reserved a space in memory for that variable and because we have named it descriptively, we now know that variable should store the value of my name.

Memory on a machine has lots of little spaces all of which are say 1 byte in size. Each variable data type is a slightly different size to another data type. Here I have used C++ to show on the screen the size of each data type to help understand this.


Using this little print out we can see how many bytes each data type will use up. So when declaring a variable of type int in C++ you can see it will use up 4 spaces in RAM as each space is 1 byte in size. A char and a bool only use up 1 space in RAM and as you can see, a double uses quite a large amount of space compared to other data types. Especially a float. This is why we advise that wherever possible you should always use a data type of float rather than a double.

Analysing that C++ code a little you can see everything should by now be familiar to you. We have however used a new function here called sizeof(), this little handy function returns the size of an object passed to it. You can see that each time I used this function I passed in a different data type. On my computer an int is the same size in bytes as a long int.

signed & unsigned

In addition to all of these data types they all come in two varieties. Signed and unsigned, Sometimes you will need negative numbers and other times you wont. int and long are assumed to be signed if the word unsigned is not used, signed integers are either negative or positive. unsigned integers are always positive. For an int or a long they both take up 4 bytes. So what use is using half of that space for negative numbers if you’re not needing negative numbers. By setting an int or long as unsigned you allow yourself a large selection of positive numbers, in fact twice the amount of a signed int or long.

Define and declare constants and variables in C++

To create or define a variable in C++ is very simple in itself. First you type the data type of the variable followed by a space and then the name of the variable followed by a semi-colon. The variable name can be anything using letters and numbers, but please note spaces are not allowed in variable names. Good variables names will be descriptive and inform the programmer what the variable will contain. For instance a variable called myAge is very descriptive and precise.

The above line of C++ defines a variable using the data type int and is called myAge NOTE: C++ is case sensitive, so myAge is a different variable to MyAge. If you use good variable names then you will need less comments in the code to help understand what is going on.

Some words in C++ are reserved and cannot be used as variable names. For a list of these reserved keywords click here. To determine which data type you should be using you can also look at our C++ data type cheat sheet by clicking here

Creating More than one variable at a time

Very simple, you can define multiple variables at once by stating the data type followed by all the variables you need of that data type all separated by commas.

Clearly here myAge and myWeight are each declared as unsigned integer variables whereas area, width and length are all defined as long integers. You cannot mix data types in one line so its only possible to define variables together that are of the same data type.


Similar to variables, constants are data storage locations. However, Where a variable is.. well.. variable meaning it can change and vary depending on different circumstances or functions within a program, a constant stays the same throughout. Constants are used for data that will and cannot be changed or altered in any way. We declaring a new constant you must initialise a value to it instantly and a new value cannot be given to it later in the program. Once it is set it is final.

Literal Constants

A value typed directly into the program wherever needed and cannot be changed.

myAge is a variable of type int and is set to 28. The value of 28 cannot be changed, It is simply 28

Symbolic Constants

A Symbolic constant is a constant represented by a name. If your program has one variable name staff and another named offices. If we knew the number of offices and the number of staff members per office is 12 then we could work out how many staff members in total there was.

staff = offices * 12;

Here 12 is a literal constant, our line of code would be easier to understand and read if we made this literal constant a symbolic constant.

If in the future the number of staff members per office changes, we could then go and change this symbolic constant where it is initialised and therefore not have to edit many lines of code in every location it is used in. To define a constant such as staffPerOffice you use a keyword called const.

This line has declared a symbolic constant called staffPerOffice and is set to type unsigned int as we do not need to have a negative number for a number of staff members.

Enumerated Constants

Enumerated constants are a set or group of constants. We could declare SIZE to be an enumeration which has 3 values SMALL, MEDIUM and LARGE. The syntax for defining an enumerated constant is as follows:

Here we are using one of C++’s keywords, enum. This is then followed by the name of the enumerated constant, in this case SIZE which is the followed by curly brackets. Inside the curly brackets are the 3 different values we have set to our size constant. SMALL, MEDIUM, LARGE.

This statement performs 2 tasks:

  1. It makes SIZE the name of the enumeration, that is, a new type.
  2. It makes SMALL a symbolic constant with the value 0, MEDIUM a symbolic constant with the value 1 and so forth.

All enumerated constants have an integer value. If not otherwise specified the value starts at 0 and counts up. You can specify what you would like each value to be, however if you decide not to do one of the values then this one will simply increase by 1 the value of the one before it.

Using the above code as an example, SMALL equals 20, MEDIUM equals 21 and LARGE equals 40. Because we did not specify what MEDIUM would be it as takes the previous value and increased by 1.

It is common practice to use all capital letters for any constant variable so when reading over code you can immediately identify that the value is a constant and not a normal variable.

Assign and manipulate and print to the screen variable values in C++

Assigning a value to a variable you would use the assignment operator, =. To assign a value of 10 to a variable called Width you would do the following:

These 2 lines of code can be combined and made shorter by doing the following:

Here I will show you small example of program that computes the area of a rectangle and prints the output to the screen.

The bare bones of the program by now you should be familiar with. Within the main function we are declaring 2 variables. Width and Length with values 10 and 15 respectively. After this we declare a short int called Area which is initialised by multiplying Width with Length. We then proceed to print out the values to the screen which can be seen here.



Programming using a lot of variables of similar types can get quite tedious and repetitive. It is possible to shortcut this using what is known as a typedef. This is a C++ keyword which stands for type definition.

It is very simple but can speed up and reduce errors within code.

This has created a new name USHORT that can be used anywhere in the code that we have previously used unsigned short int. The folowing section of code is the same as before but using this new typedef:


This lesson was quite long winded but this has got a lot of the basics out of the way. Soon we will be moving onto some more fun areas of C++. Please feel free to use the comments below if you have any questions and I will gladly help you out

Add a Comment

Your email address will not be published. Required fields are marked *