Type Systems

Learn about type systems in modern programming languages.

Variable types

It is a common practice to use the static type system in compiled programming languages, such as C. When using this system, we decide how to store the variable in memory. We should specify the variable type when declaring it. Then, the compiler allocates memory and picks one of the predefined formats to store this type of variable.

Here is an example of how the static type system works. Let’s suppose we want to declare a variable called number. We should specify its type in the declaration. We choose the unsigned integer type, which has a size of two bytes. Then, the compiler allocates exactly two bytes of memory for this variable.

When the application starts, we assign the 203 value to the variable. It is equal to 0xCB in hexadecimal. Then, the variable looks like this in the memory:

00 CB

Modern computers use the binary form to store information in the memory. Here, we use the hexadecimal format instead for clarity.

Value of 203 in different number systems

One byte is enough to store the 203 value. However, we forced the compiler to reserve two bytes for that. The unused byte stays zeroed. No one can use it in the scope of the number variable. If the variable has a global scope, the byte is reserved and unused while the application works.

Let’s suppose that we assigned the 14037 value to the variable. It is equal to 0x36D5 in hexadecimal. Then, it looks like this in the memory:

36 D5
...