Although the previous chapters have analyzed the variables handled by the language C and the corresponding data types, some characteristics related to the variables have not been analyzed, the most important being the duration and scope. Combining these features distinguish the different types of C storage: auto, static, extern and register.
A fixed variable arises at the beginning of the program and survives until the completion of its execution, constantly invading its memo part. On the contrary, an automatic variable is associated with a specific function or block of the program. Therefore, this function or block will only be created when it starts to run and will disappear upon completion of block execution. Consequently, an automatic variable can be generated several times and if the block or function corresponding to the variable is not used in a execution, the variable will not be generated.
This has a great influence on initialization, since as long as the fixed ones have a single initialization, the automatic ones will be initialized every time the function or block is referenced externally.
As we will see later, global variables will be fixed and local ones will be automatic.
The scope is the concept corresponding to the source program and represents the field of knowledge of the name of a variable. In language C, four types of knowledge areas are distinguished:
The scope of the variable depends on where it is located, although the static can change the keywords.
Here are some examples to illustrate the above:
a)
These two automatic variables (called i) are different because they have the scope of the block.
All this is due to the location of the definitions, being inside the function the variable, unless otherwise stated, is automatic and its field is the lock.
b)
j)
We have two variables called, one global, another external (fixed) and another local, included in function f1.
The first three results are due to the execution of function f1 (initialization is Ø when defined exclusively) and the last to the main program.
In some high-level languages two types of storage are distinguished: global and local. In C it is possible to combine the duration and scope of variables, obtaining different types of storage, taking into account the location of the variable definition and the use of the keywords extern, static, auto and register.
Let's look at the meaning of the following indicators:
external:
when you want to use a global variable outside the module in which it is defined, a new estimate of the variable is made, but predefined the extern indicator, indicating that the definition is elsewhere.
static:
has double function. Reduces program view to file to externally defined (global) variables. Changes the duration of those defined within the functions to fixed.
car:
adapts to variables with block frame but defined within functions. Since these variables, if they do not fit any indicator, adopt the same characteristics as the auto indicator, this indicator is practically never used.
register:
This indicator can only be used with variables with block view and its incidence in runtime is the location of the variable in a general record. This allows to speed up the execution of the program by adapting it to widely used variables.
Figure 1 shows a summary of all features.
Once we analyze all the characteristics, we will see the use of the different types of variables exposed. We can distinguish four different uses:
Figure 2 shows an example in which the new type of stack data that is defined in it is only externally accessible by push, pop and clear functions.
In the module in which one of these functions is used a mention will be made specifying the external indicator. For example to use push: extern void push ().
The variables seen so far are static, that is, the compiler will know the amount of memory they occupy and therefore can predict their location.
Dynamic memory is interesting when the length of the data we handle is variable depending on the execution. Pascalez demonstrators are dynamic, as they are requested and released by new and dispose functions. In C there is a mechanism of acquisition and release of dynamic memory through the functions of library malloc, calloc, realloc and free that will be exposed in chapter 11.