Linux Index

C Memento



Declarations & definitions

Functions can be declared at the beginning of the file and defined later in the file. This avoid the automatic conversion of types (of the arguments and the returned value) to standard types when this function is called. The default return type of a function is int.

External variables and visibility

The variables which are declared outside of any function are external variables. Some external variables are 'static' which means that their visibility is limited to the current file. All external variables are created by the initialisation of the program and are stored on the data segment.

When a variable is defined in a function its visibitily is limited to this function. When its definition is prefixed by a 'static keyword', it's a static variable and it is also stored in the data segment. All other local variables are stored on heap and are created at the time that the function is called (it is a dynamical creation).

The variables which are stored on the data segment can only be initialised by a constant whereas the local (but not static) variables which are stored in the heap can be initialised dynamically by a variable's value.

The visibility of an external variable or a function begins at the place where it is declared.

To use a global variable in a function, it should be declared as 'extern' in this function, unless this variable has already been declared previously in the same file.

To make the distinction between the declaration and the definition of an external variable, we use the keword 'external' for the declaration and we omit this keword for the definition. For example,


int num;

is the definition of the external variable 'num'. In the next example,


external int num;

it is only the declaration of the variable 'num'.

If an external variable is used in different files, its definition may only appear in one file, while the declaration has to be repeated in all the other files. To face this situation, one includes a common 'header' file at the beginning of every source file. This header file contains the declaration of all the external variables and functions.

For functions, the distinction between a declaration and a definition is more obvious and the kewords 'external' should not be used.

Static variables

Some variable are defined with the keword 'static' in prefix. These variables are stored on the data segment as any external variables. They are created and initialised by the start of the program. But the keword 'static' means that these variables are more local than other variables. There are two cases:

  • outside of any function, a static declaration means that the visibility of the variable is limited to the current source file;
  • in the body of a function, a static declaration means that the value of this variable will be conserved after we retured from this function and it will be reusable the next time we call the function.

Local variables

At the beginning of each block (delimited by '{' and '}'), a local variable can be defined and created dynamically. This variable is only visible in this block. This situation appears frequently at the beginnig of any function but can also appears in any sublock as in the following example:


if (a < b) {
   int i=3;
   printf("%d", i);
}