Programming is above all giving orders to our computer so that it achieves what we want. These orders will allow our computer to manipulate information in various forms (numbers, texts, videos, etc.). At this point, we know that these orders, these instructions are executed by our processor. However, we still do not know how to give orders, or how to handle information.
This chapter will explain how to manipulate the simplest types of data in the C language, numbers and letters (or characters), using so-called variables. After this one, you will be able to enjoy your computer as if it were a big calculator. Nevertheless, rest assured, the math level of this chapter will be quite low: if you know how to count, you can easily understand it!
It may sound a bit silly and not very interesting, but you have to start with the basics. Manipulating text or video is complex and requires knowledge of how to manipulate numbers. Yes, I did. As you will see, everything is a number for our computer, even text and video.
What Is A Variable?
To understand what a variable is and how to manipulate it, you have to start by understanding how our computer stores data. In theory, a computer is capable of storing any type of information. But how is it possible to achieve such a miracle when it is ultimately only a cluster of electrical circuits?
You may have heard the saying: “If the only tool you have is a hammer, you will see any problem as a nail” (Abraham Maslow). Well, the idea is a bit the same for a computer: the computer only knows how to use numbers, he sees all information as a series of numbers.
The trick is to turn information into numbers so that the computer can process it, in other words to digitize it. Different techniques are possible to achieve this goal, one of the simplest being a matching table, for example between a number and a character
However, as if that were not enough, a computer does not count like us: it has base two.
The base is the number of numbers available to represent a number. On base 10, we have ten digits: zero, one, two, three, four, five, six, seven, eight and nine. In base two, we have… two: zero and one. In terms of counting, it’s the same: we start by depleting the units: 0, 1; then we move on to the dozens: 10, 11; then to the hundreds: 100, 101, 110, 111; and so on. Below is a small matching table between base two and base ten.
|Base two||Base Ten|
A binary number (a zero or a one) is called a bit in English. This is the contraction of the term “binary digit.” We will use it quite often in the course for the sake of economy.
But why use base two and not base ten?
Because the data flows in the form of electrical currents. However, since the voltage of these is not always stable, it is difficult to achieve a reliable system that can detect ten different values. On the other hand, it is perfectly possible with two values: there is current or there is none.
We now know that our computer can only use numbers represented in base two.
But how do you store all this clutter of numbers?
Well, the bits are stored in a particular electronic component of the computer: memory. Finally, we say “memory,” but there are actually several.
But why several memories and not one?
The fact is that it is currently impossible to create memories that are both fast and capable of containing a lot of data. So we can’t use a single large memory that can store all the data we need. This problem has arisen from the very beginning of computer science, as evidenced by this quote from the 1940s, from the designers of one of the very first computers.
But researchers and engineers at the beginning of computing found a solution: segment the computer’s memory into several sub-memories, of different size and speed, each used as needed. So we’ll have memories that can hold little data and fast, alongside larger, slower memories.
We told you that the computer used several memories. Three of them deserve your attention:
- Registers are memory built into the processor, used to store temporary data. They are very fast, but can only contain very simple data, such as numbers.
- RAM is a slightly larger memory, but slower than registers. It can hold quite a bit of data and is usually used to store programs running and the data they manipulate.
- These two memories (registers and RAM) still have a slight flaw: they lose their contents when they are no longer fed. Suffice to say that this is not the best place to store an operating system or personal files. This is the role of the hard drive, a memory with a very large capacity, but very slow which has the advantage of ensuring the persistence of the data.
Bits, multiplets and bytes
In RAM, the bits are grouped into fixed-quantity “packages”: “memory boxes,” also known as multiplets. With a few exceptions, memories use eight-bit multiplets, also known as bytes. A byte can store 256 different information (you can do the calculation yourself: how much is 111111111 in base two?). To store more information, it will be necessary to use several bytes.
Nevertheless, it is all very well to store data in memory, but it is necessary to be able to get your hands on it.
With this in mind, each multiplet of the RAM is assigned a unique number, an address, which will allow it to select and identify it among all the others. Imagine the computer’s RAM as a huge cabinet, which would contain many drawers (memory boxes) that can each hold a multiplet. Each drawer is assigned a number to recognize it among all the others. We could ask what is in drawer number 27. For memory, it’s the same. Each memory box has a number: its address.
In fact, you can compare an address to a phone number: each of your correspondents has a phone number and you know that to call a particular person, you have to dial that number. Memory addresses work exactly the same way!
More generally, all memories have a similar mechanism for finding the data. Also, you will often hear the term reference for a means (such as an address) to locate a data. It is simply a more general concept.
So What is a Variable?
All this is very nice, but explicitly manipulating references (addresses if you prefer) is a real ordeal, as is trying to calculate on base two. Fortunately for us, programming languages (especially C) are responsible for converting for us and replacing references with variables.
A variable will correspond to a portion of memory, called an object, to which we will give a name. This name will identify our variable, just as a reference can identify one portion of memory among all the others. This will allow us to name the data we manipulate, each of which is replaced when compiling by a reference (usually an address).