In the variable declaration you can also define
lifetime or storage duration of the variable. Lifetime indicates the
length of time the variable value is guaranteed during execution. For example,
if the variable is defined inside the function, its value is kept until the
function executes. After completion of the function, the storage allocated for
the variable is freed.
Program
#include <stdio.h>
int g = 10;
\\ A
main()
{
int i
=0; \\ B
void
f1(); \\ C
f1(); \\ D
printf(" after first call \n");
f1(); \\ E
printf("after second call \n");
f1(); \\ F
printf("after third call \n");
}
void f1()
{
static
int k=0; \\ G
printf("value of k %d j %d",k,j);
k=k+10;
}
Explanation
- Variables in C language can
have automatic or static lifetimes. Automatic means the variable is in
existence until the function in which it is defined executes; static means
the variable is retained until the program executes.
- The variable that is defined
outside the function, such as g in statement A, is called a global variable
because it is accessible from all the functions. These global variables
have static lifetimes, that is, variable return throughout the program
execution. The value of the variable, as updated from one function, affects
another function that refers to that variable. It means that the updating
in this variable is visible to all functions.
- Variables such as i, defined in main, or j, defined in f1, are of the automatic type;
i exists until main is completed and j exists until f1 is completed.
- You can define the lifetime
of a local variable in a function as given in statement G. The variable k has a static lifetime; its
value is returned throughout the execution of the program.
- The function f1 increments the value of k by 10 and prints the values
of j and k.
- When you call the function
for the first time using statement D, k is printed as 0, j is printed as 10, k is incremented to 10, the
space of j is reallocated, and j ceases to exist.
- When you call the function
the second time, it will give 10 (the previous value of k) because k is a static variable. There
are reallocations for j so j is printed as 10.
- When you call the function
the third time, j is still printed as 10.
Points to Remember
- The variables in C can have static
or automatic lifetimes.
- When a variable has a static
lifetime, memory is allocated at the beginning of the program execution
and it is reallocated only after the program terminates.
- When a variable has an
automatic lifetime, the memory is allocated to the variable when the
function is called and it is deallocated once the function completes its
execution.
- Global variables have static
lifetimes.
- By default, local variables
have automatic lifetimes.
- To make a local variable
static, use the storage-class specifier.
In
collaborative software development it is common for multiple users to write
programs in different files. For example, one user implements function f1, a second user implements function f2, while a
third user implements the main function. C has a provision to compile programs
even if function or variable implementation is not available. In such cases,
the program is compiled but it is not yet fit for execution. The program is not
executable until all the references in the file are available.
Program
\\ Program in file externa1.c
#include <stdio.h> \\ A
#include <d:\cbook\storage\f1.cpp> \\ B
extern int i; \\ C
main()
{
i
=0;
\\ D
printf("value of i %d\n",i);
}
\\ Program in file f1.cpp
int i =7; \\ E
Explanation
- Here the program is written in two files: extern1.c and
f1.cpp. The file extern1.c has
the main and reference of variable i.
- The file f1.cpp has the declaration of i.
- In the file extern.c there is a reference of i so the compiler should know
the data type of i. This is done using the
extern definition by statement C. Extern means that the variable or
function is implemented elsewhere but is referred to in the current file.
- Statement D refers to i.
- The definition of i is given in the file f1.cpp, as given by statement E.
- In the absence of an include directive in statement B,
you can still compile the file; it will give no errors. Such a file is
called an object file. It is not fit for execution because
the reference of i is not resolved.
- When you write statement B
the reference of i is re-sorted and the
executable file can be made.
Points to Remember
- Extern definition is used
when you have to refer a function or variable that is implemented
elsewhere or it will be implemented later on.
- When all the references are
resolved then only the executable file is made.
When you
want to refer a variable, many times you can allocate fast memory in the form
of a register to that variable. For variables such as loop counters, register
allocation is done. The processor has memory in the form of register for its
temporary storage. The access time of the register is much less than main
memory. That is the reason that register allocation provides more speed. But
the processor has a limited number of registers. So the register declaration
acts as a directive; it does not guarantee the allocation of a register for
storing value of that variable.
Program
#include <stdio.h>
main()
{
register
int i = 0; \\ A
for(
i=0;i<2;i++)
{
printf("value of i is %d\n",i);
}
}
Explanation
- Here the register allocation
directive is given for variable i. During execution, i will be allocated a
register if it is available; otherwise, i will receive normal memory allocations.
- You can use a register
directive only for variables of the automatic storage class, not for
global variables.
- Generally, you can use
register storage for int or char data types.
Note
|
You
cannot use register allocation for global variables because memory is
allocated to the global variable at the beginning of the program execution.
At that time, it is not certain which function is invoked and which register
is used. Function code may use the register internally, but it also has
access to a global variable, which might also use the same register. This
leads to contradiction, so global register variables are not allowed.
|
Points to Remember
- Register allocation is done
for faster access, generally for loop counters.
- You cannot declare global
register variables.