Storage of Variables

STORAGE

Introduction

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 
int g = 10; \ A
main()
{
 int i =0; \ B
 void f1(); \ C
 f1(); \ D
 printf(" after first call 
");
 f1(); \ E
 printf("after second call 
");
 f1(); \ F
 printf("after third call 
");

}
void f1()
{
 static int k=0; \ G
 int j = 10; \ H
 printf("value of k %d j %d",k,j);
 k=k+10;
}

Explanation

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. The function f1 increments the value of k by 10 and prints the values of j and k.
  6. 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.
  7. 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.
  8. When you call the function the third time, j is still printed as 10.

Points to Remember

  1. The variables in C can have static or automatic lifetimes.
  2. 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.
  3. 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.
  4. Global variables have static lifetimes.
  5. By default, local variables have automatic lifetimes.
  6. To make a local variable static, use the storage-class specifier.

EXTERNAL REFERENCES

Introduction

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  \ A
#include  \ B
extern int i; \ C
main()
{
 i =0; \ D
 printf("value of i %d
",i);
}

\ Program in file f1.cpp

int i =7; \ E

Explanation

  1. 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.
  2. The file f1.cpp has the declaration of i.
  3. 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.
  4. Statement D refers to i.
  5. The definition of i is given in the file f1.cpp, as given by statement E.
  6. 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.
  7. When you write statement B the reference of i is re-sorted and the executable file can be made.

Points to Remember

  1. Extern definition is used when you have to refer a function or variable that is implemented elsewhere or it will be implemented later on.
  2. When all the references are resolved then only the executable file is made.

REGISTER VARIABLES

Introduction

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 
main()
{
 register int i = 0; \ A

 for( i=0;i<2;i++)
 {
 printf("value of i is %d
",i);
 }
}

Explanation

  1. 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.
  2. You can use a register directive only for variables of the automatic storage class, not for global variables.
  3. 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

  1. Register allocation is done for faster access, generally for loop counters.
  2. You cannot declare global register variables.

SCOPE OF VARIABLES

Introduction

In C you can define a variable in the block. The blocks are marked using { and } braces. The blocks can also be defined using the for statement. The scope of the variable is in the block in which it is declared, meaning that you can use that variable anywhere in the block. Even if some block is declared in that block, you can use that variable. When the variable is referred in the block and if it can be resolved using two definitions, then the nearest definition has more precedence. So the variable is interpreted according to the nearest definition. Even if the two definitions define two different data types for variables, they are accepted.

Program

#include 
main()
{ \ Block 1
 int i = 10; \ A

 { \ Block 2
 int i = 0; \ B
 for( i=0;i<2;i++) \ C
 {
 printf("value of i is %d
",i);
 } \ End of block 2
 }
 printf("the value of i is %d
",i); \ D
} \ End of block 1

Explanation

  1. The statement block 1 defines the start of block 1.
  2. The statement ‘end of block 1’ defines the end of block 1.
  3. Statement A defines variable i which has the scope in the entire block 1.
  4. The statement block 2 defines the start of block 2.
  5. The statement ‘end of block 2’ defines the end of block 2.
  6. Statement B defines variable i which is entirely in block 2.
  7. The for loop refers i, which can be resolved using two definitions: statement A and statement B.
  8. Since the definition of statement B is nearest, the variable is referred using that definition, so the for loop modifies the value of i at statement B.
  9. Variable i at statement A and variable i at statement B are two independent variables even though they have the same name. Statement D is outside block 2, so it prints the value of variable i in block 1.

Points to Remember

  1. In C, you can define variables in the block, which is demarcated by using { and } braces. The variable has the scope inside the block in which it is declared.
  2. When the variable is resolved using two definitions, the nearest definition has more precedence.

FURTHER SCOPE OF VARIABLES

Introduction

In C, you can define the counter in the for loop itself; the counter has scope up to the end of the for loop.

Program

#include 
main()
{
 int k = 10;

 }
 for(int i=0;i<2;i++)\ A
 { \ B
 printf("value of i is %d
",i); \ C
 } \ D
 {
 printf("the value of i is %d
",i); \ E
}

Explanation

  1. The counter variable i is defined at statement A.
  2. The scope of the for loop is up to statement D, which is the end of the for loop for statement B.
  3. If you do not comment out statement E, you will get an error, because you cannot refer i in the outside block.

Point to Remember

You can define counter variable inside the for loop.



C & Data Structures
C & Data Structures (Charles River Media Computer Engineering)
ISBN: 1584503386
EAN: 2147483647
Year: 2006
Pages: 232

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net