Learn C++ with Qt, Part 005: Definition of Variables

In this part of my C++ with Qt tutorial, I’d like to expand upon the area of data definitions, more specific the definition of variables. In the previous part about data input I already used some variable definitions in order to store and use the data input by the user within the program.

This is more of a background article, but it should give you a better understanding of what is happening inside the computer when your program is running. The additional knowledge can help you to avoid problems or find the source of bugs in your programs later on.

So let’s have a closer look at the definition of some commonly used types of variables…

Data definitions – a short introduction

In order for the computer to do anything with a value that is either entered by a user, predefined in the program’s code or calculated, we need to define a variable that can store the value.

While not all data has to be entered manually by the user, programs need clearly defined memory regions in the RAM where data can be stored accessed temporarily. The computer’s central processing unit (CPU) has some memory registers, but not enough to store all data that is necessary for a more complex application.

The technical view

On the technical side, the definition of a variable makes the computer reserve a part of its main memory and assigns a fixed memory address to the name of the variable. The amount of reserved memory depends upon the type of the variable – a single character needs less memory than a whole string of characters, and a small integer number needs less space than a long number, just to name two examples.

When the program accesses the variable – or more exactly, tries to read or change the value that is stored in this variable – the computer looks up the current value at the memory address that is associated with this variable, and reads or changes it from there.

It is important to remember that the computer assigns a memory address to the variable that marks the start of the memory area where the value of the variable is stored when the program is running. The value of the variable usually fills up a small block of successive memory addresses, right up to its maximal length or up to a special end marker value (for strings).

The memory address of a variable can be accessed by using a pointer, which can be useful for converting values between variables of different types. I’ll tell you more about pointers in a later part of this tutorial.

How to define a variable

In the C++ programming language – like in a lot of other programming languages – variables have to be properly defined before they can be used. A proper variable definition includes the data type and the name of the variable. In C++ (and other variations of the C programming language), the data type is written first, before the name of the variable, e.g.:

int x;

In the above example, the variable with the name “x” is defined as an integer variable (identifier “int”). This means that “x” can contain a positive number value where the numbers are integer values without any decimal places.

This form – a single data type identifier followed by the variable name and a semicolon – is the shortest and easiest type of a data definition in C++.

In the C++ programming language, variables and constants are defined right at the beginning of a function, method or object class, before any further instructions.

If you try to use a variable or constant before having defined it, you get an error when you try to compile the program.

Assigning values to variables

Once a variable is defined, it can be used in the program. If you want to assign a specific value to the variable, this can be done in a way that is similar to a mathematical equation. The following line of code stores the value “5” in the variable “x”:

x = 5;

It is also possible to combine the variable definition with the assignment of an initial value:

int x = 5;

The memory reservation and storing the value in the newly reserved memory area are combined into one line of code, although the computer will execute both steps one after the other (just like it would be when you split up the variable definition and the value assignment into two separate lines of code).

In the same way, a computed value can be assigned to the variable:

x = x + 4;

The statement above would instruct the computer to first add 4 to the current value of x (right side of the assignment) and then store the new value in x. The new, computed value overwrites the old value as it is stored in the same memory location as the old value for x.

Functions and methods can also be a source for a new value that can be assigned to a variable. Some functions return a value just like a direct computation:

y = max(a,b);

In this example, the variable “y” is assigned the return value of the function “max” which determines the maximum of the values stored in the variables “a” and “b”. So in the end, “y” contains the maximum of “a” and “b”.

Some functions and methods also use reference variables as part of their input, so that more than one value can be returned. So in a function call like

y = MyComputation(a,b,c);

the Function “MyComputation” not only returns a value which is then stored in the variable “y”, but it also can change the values for the input variables “a”, “b” and “c”, if these have been defined as reference variables or pointers. More on that later…

Initial values for variables

In the C and C++ programming language, you can combine the definition of a variable that uses one of the standard data types with the assignment of an initial value for that variable, e.g.:

int x = 5; char bval = 'X';

If your code gets longer and more complicated, you can use this to make sure that certain variables have useful values when they are used the first time. Alternatively, you can put in a special code section, function or method with the assignment of initial values for these variables and use this at the start of your program.

Using variables without inital or assigned values can cause problems. For character-based variables, this is usually no big problem, but for integer or floating point values an "empty" variable without a value can lead to wrong computations or even a breakdown of your program during runtime.

Value limits

Due to the reservation of a fixed amount of memory for a defined variable, the variable itself can only contain a certain range of possible values. These values also depend on the basic binary nature of the inner workings of the computer.

This means that an integer variable like the one defined by

int x;

cannot be as big as you like, only as big as what can be stored inside the reserved part of the memory.

In the case of an integer variable defined with the identifier “int”, this typically means a 16-bit (2-byte) value (at least right now, in autumn of 2014). Each bit can only have one of two values, namely “0” (zero) or “1” (one). With 16 bits, the amount of possible values is 2 to the power of 16 (2^16), which is equal to 65536 different values. As the values start with zero (in the form of 16 single zeroes), the highest possible value is 65535.

Bigger values can be stored and used by using different data types like “long int” (which uses double the amount of memory than a normal “int”).

Here is a short overview for typical value ranges depending on the amount of memory that different data types use:

of bits
Amount of possible values Remarks
8 256 equal to 2 to the power of 8
16 65 535
32 4 294 967 296 more than 4 billion values
64 18 446 744 073 709 551 616 that's around 18 billion billion possible values!


That’s all for now concerning the basics of data definitions. In the next part of this tutorial, we take a closer look at the different standard data types that you can use when defining variables or constants.

Stay tuned!

Enjoy this page? Please pay it forward. Here's how...

Would you prefer to share this page with others by linking to it?

  1. Click on the HTML link code below.
  2. Copy and paste it, adding a note of your own, into your blog, a Web page, forums, a blog comment, your Facebook account, or anywhere that someone would find this page valuable.