Variables

Location
  1. Courses

    /

  2. Complete C++ Course

    /

  3. The basics

    /

  4. Variables

    /

  5. What is a variable

What is a variable?

A variable is a reserved memory space used to store and retrieve information. It has a type which specifies how many bytes it is made of and how to interpret them. It also has a name that is used to refer to it. It is way more convenient to write "Set the value of the variable named level to 8" than "Set the 4 bytes at the address 573249 to the value 8".

Only numbers can be stored in variables. That is because the only things computers can handle are numbers. That, however, does not mean variables can only represent numbers. How so? Well, as an example, if we decide that, for a variable named color, the number 0 means blue and the number 1, green, then if that variable holds the value 1, it represents the color green. If we decided that the number 0 represents the letter a, the number 1 b and so on, then the set of number (18 10 24) would represent the word sky. It is all about interpretation.

Byte

Octet is a word used to represent a group of 8 bits. Nowadays, almost all computers have a byte size of 1 octet. However, a byte could technically weight any number of bits. The definition of byte is "the smallest unit of memory a computer can natively handle".

Primitive categories

The basic type of variables are called primitives. They can be divided into 3 categories: integer, unsigned integer and floating point primitives.

The only difference between the primitives of the same category is their size in bytes.

Note that the same primitive may have a different size depending on the compiler used. For example, a variable of type long may be composed of 32 bits when using a compiler and 64 bits on an other.

Defining a variable

To define (Create) a variable, we must first write its type, then its name and finally a semicolon ;.

Example:

int variableName;

The effect of the line of code above is to reserve a memory space of the size of the type int and tell the compiler that this memory space will be referred to with the name variableName. Because of the type of the variable variableName, which is a primitive of the category integer, the bits that compose it are interpreted as an integer that can be either positive or negative.

The representation of positive integers

We can assign a value to a variable by writing its name followed by the equal sign =, then the value and a semicolon ;.

Example:

int number; number = 9;

If we consider that the type int weights 4 bytes (With bytes made of 8 bits, so a total of 32 bits), then, after the line of code above, the bits of the memory space represented by the variable number would become:

00000000000000000000000000001001b (9 in decimal).

Separated in bytes:

00000000 00000000 00000000 00001001b

Since the number of digits (bits) inside the memory space of a variable is constant, no matter the size of the number it holds, all the bits are used (Which is why, in the example above, there are zeros at the left of 1001b). That makes it obvious that depending on the size of a primitive, the range of number that can be represented changes.

The representation of negative integers

Above, we saw the representation, in memory, of the positive integer 9. That was pretty straightforward. The representation of negative numbers is, however, a bit more complex. There is no negative sign - used to tell negative numbers apart from positive ones. Negative numbers are (Usually, but that may be different on some processor architectures) represented using the Two's complement notation. To represent a negative binary number with that notation, we take the positive equivalent of that number, invert its bits (Ones become zeros and zeros become ones) and then add 1 to it.

As an example, let us represent the number -9 in binary using the Two's complement notation.

Here is the positive equivalent of -9 (which is 9 in decimal) in binary:

00000000000000000000000000001001b

Here is the number with its bits inverted:

11111111111111111111111111110110b

The number added with 1:

11111111111111111111111111110111b

Here is the number -9, in binary, represented using the Two's complement notation:

11111111111111111111111111110111b

That means that if we write the following line of code:

number = -9;

The bytes of the variable number would become:

11111111 11111111 11111111 11110111b

Again, this is true only if the type int has a size of 32 bits. If it has a size of, for example, 16 bits, the bytes of the memory space would be the following:

11111111 11110111b

The representation of floating point numbers

The representation of floating point numbers is more complex, so we will see that later. For now, note that they are stored using the scientific notation (Example: 2.56 * 104). Unlike with variables of signed or unsigned integer type, which store the exact value of the numbers, variables of floating point type may approximate the numbers they hold, especially when they are big in term of number of digits required to represent them.

Naming rules

A variable may be named using letters, numbers and underscores _. It can not contain spaces, symbols (#, % +, -, *, &, ...) nor begin with a number. Those naming rules are actually the same for all C/C++ elements.

Valid nameInvalid name
var1var
var_namevar name
number1var%

Naming conventions

Programmers follow different naming conventions (Rules about how to name specific elements). Some may decide to name all variables in lowercase and use underscores between the words that compose it: my_variable_name.

In this course, variables are named in camelCase. That means they are written in lowercase and the first letter of each word is written in uppercase (Except the first word): myVariableName. The advantage of camelCase over using underscores is that it is more compact.

The naming convention followed does not matter so much. What really matters is to be consistent and that everybody working on a same project follow the same naming rules.