Programming language C (IX). Characteristics of variables

Some characteristics relating to the variables handled by language C remain to be analyzed, the most important being duration and scope.

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.

Duration: Fixed vs Automatic

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:

  1. program: the variable name is global, so it can refer to any function, block or order.
  2. from the file: it is only known in the source file in which the variable name is defined. Therefore, when using distributed compilation, if referenced from another module it will not be recognized, since the compiler will take it as another variable.
  3. function: the variable name is only known in the function or subprogram itself.
  4. the block: a block is the one between the same and the corresponding ones} and is only known in the block in which the variable is defined.

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:


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.



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.

Storage types

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:


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.


has double function. Reduces program view to file to externally defined (global) variables. Changes the duration of those defined within the functions to fixed.


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.


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.

Figure . Duration and scope of variables according to the indicator. (Note: To see the image go to pdf).


Once we analyze all the characteristics, we will see the use of the different types of variables exposed. We can distinguish four different uses:

  • Automatic local variables: they are the most used, since they have the block field and the automatic duration. The definition is performed within the functions, without any indicator or with a register indicator.
  • fixed local variable: if a variable used in a single function or block needs to maintain its value, from one call to another, it will be of this type. The definition is done the same as the previous one but using the static keyword.
  • Simple global variable: it is characterized by knowing and maintaining the value from different functions or blocks. When defining, no indicator should be placed, but they are defined separately from the functions. However, when referenced from another module or file, the variable must be referenced by specifying the external indicator. Most functions (as they adapt to a type of function) are usually of this type.
  • isolated global variable: when defining new data types it is very interesting that the variable is captable of the existing functions in the module that defines the data type, but not of other modules. The duration is fixed and the scope of the file is very interesting. The definition is done the same as the previous one, but prepends the static indicator.
Figure . Definition of a new type of data.

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 ().

Dynamic memory

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.

Jarrai iezaguzu

Zu idazle

Zientzia aldizkaria

azken alea
Eusko Jaurlaritzako Industria, Merkataritza eta Turismo Saila
MAIER Koop. Elk.
KIDE Koop. Elk.
ULMA Koop. Elk.
EIKA Koop. Elk.
LAGUN ARO Koop. Elk.