Language C: Variables
- What is a variable?
To understand what a variable is, and how to manipulate it, one must begin by understanding how our computer does to store this basic information. Our computer has been designed to be versatile enough: it can theoretically store any type of information. To do this, it uses a particularly simple trick: it stores its information by cutting into small units of information called bits. These bits are therefore very simple units that can not take two values: 0 or 1.
To store information, just take several of these bits and group them next to each other. By doing so, we can create sequences of 0 and 1 that can be interpreted as numbers. We can thus represent positive numbers, negative numbers, decimal numbers, etc. All that our computer can do is manipulate these sequences of bits, these numbers. In short, our computer is only a big calculator.
More complex information, such as video, text, etc. are all stored in our computer in the form of numbers. By using several of these bits, we can represent anything: text, numbers, video, etc. I guess it will be difficult to believe me, but be aware that any information found in our computer is represented with only 0 and 1!
- Memory
These bits are stored in a particular electronic component, present in our computer: the memory. Its role: to store all bits that represent our information.
Finally, I say "memory", but in fact there are several. Just as a human has several memories (short-term memory, long-term memory, etc.) that are used to store a lot of information, the computer also uses several memories to store a lot of data of different sizes. .
But why many memories and not one?
The fact is that if we wanted to use a single large memory in our computer, it would be inevitably very slow: it is impossible to create memories that are both fast and can contain a lot of data. We can not use a single large memory capable of storing all the data we need. This problem arose from the beginning of computer science. The inventors of the first modern computers were soon confronted with this problem. For those who do not believe me, take a look at this 1940s quote from a research report on one of the world's first computers:
Ideally, we would like a memory of an infinitely large capacity such that any data is immediately accessible. We are forced to recognize the possibility of building a hierarchy of memory, each with a capacity greater than the previous one, but accessible less quickly.
As we can see, this quotation (translated from English) shows the problem, but also evokes the solution adopted to this problem. To solve this problem, it is enough to segment the memory of the computer into several sub-memories of different size and speed that are used as needed. We will have memories that can contain few data in which we can read and write quickly and more important memories, but slower. This solution was the first solution invented to solve this problem and is still massively used at the moment: we have not done better!
We said that the computer used several memories. And you should know that three of these memories are important, and must be known to any programmer. I present to you:
The registers ;
RAM ;
the hard drive.
So obviously, these are not the only ones: we could also mention the cache memory and more, but it has nothing to do in a tutorial on C. And then there are already courses on this subject on the Zero Site, for example, the operation of a computer from scratch.
Registers are memories built into our processor. They are very fast, but can only contain very simple data: it is difficult to put more than a number in it. Their purpose is to store temporary data for faster access.
RAM is a little bigger memory, and slower than the registers. It can hold a lot of data, and it is generally used to store the program that has just been launched, as well as the data it will manipulate.
This memory has a slight defect: it loses its content when the power is turned off. In other words, we must find another memory to store our operating system, our programs, etc. : it's the role of the hard drive, a very big memory, but very slow.
In C, the most used memory is RAM. And so, to understand how to program in C, it will understand how to interact with this RAM. Later in this lesson, we will also see how to handle files on the hard disk. But as far as the registers are concerned, it's another thing: the C almost completely hides the management of these, which is done almost entirely by the compiler. Can not handle them directly!
Declare a variable
Let's get to the heart of the matter by learning to declare our variables. To get started, you need to know that a variable consists of two mandatory elements:
An identifier: this is basically the "name" of the variable;
A type.
The type of a variable makes it possible to indicate what one wants to store: an integer, a number with comma (one says also a floating point), a character, etc. To specify the type of a variable, we must use a keyword, specific to the type that we want to give to our variable.
Once we have decided the name of our variable, as well as its type, we can create it (we also say declare it) like this:
identifier type ;
Clearly, just place a keyword indicating the type of the variable, and place the name that was chosen immediately after.
Pay close attention to the semicolon at the end!
Types
As said before, a type makes it possible to indicate to the compiler which type of data one wants to store. This type will make it possible to specify:
all the values that the variable can take;
and the operations that can be performed on it, so as not to add a letter with a decimal number.
Defining the type of a variable makes it possible to specify its potential content and what can be done with it.
The C language provides 8 basic types:
Type contents
tank a character or an integer
shorts an integer
int an integer
long an integer
float a floating point number
double a floating point number
long double a floating point number
The short, int, and long types all serve to store integers that can take positive, negative, or null values. They say that they are signed types. For these three types, there is an unsigned equivalent type. An unsigned integer type is an integer type that accepts only positive or null values: it can not store negative values. To declare variables of an unsigned type, you just need to precede the name of the integer type of the unsigned keyword.
The tank can also be used to store numbers. It is mostly used for storing characters, but these are stored in the computer as numbers, it is possible to store numbers in a tank. The only problem is that this tank may very well be signed on some compilers and not on others. Depending on the compiler used, the char will be either signed by default or unsigned. To avoid trouble using a char as a number, you can explicitly declare your unsigned chars: unsigned char or signed: signed char.
Capacity of a type : NEXT Chapter
Aucun commentaire:
Enregistrer un commentaire