Thursday, December 17, 2009

6.4 STATIC VARIABLES

In this section and the next, we make the distinction between a single-file program,in which the entire program is contained within a single source file, and a multiple program,where the functions that make up the program are contained in separate source files.the rules governing the static storage class are different in each situation.

In a single-file program,static variables are defined within individual functions and therefore have the same scope as automatic variables;i.e. they are local to the functions in which they are defined.Unlike automatic variables,however,static variable retain their values throughout the life of the program.Thus,if a function is exited and then re-entered at a later time,the static variables defined within that function will retain their former values.This feature allows function to retain information permanently throughout the execution of a program.

Static variables are defined within a function in the same manner as automatic variables,except that the variable declaration must begin with the static storage-class designation.Static variables can be utilized within the function in the same manner as other variables.They cannot,however, be accessed outside of their defining function.

Initial values can be included in the static variable declarations.The rules associated with the assignment of these values are essentially the same as the rules associated with the initialization of external variables, even though the static variables are defined locally within a function.In particular-
1. The initial values must be expressed as constants, not expressions.
2. The  initial values are assigned to their respective variables at the beginning of program execution.The variables retain these values throughout the life of the program, unless different values are assigned during the course of the computation.
3. Zeroes will be assigned to all static variables whose declarations do not explicit initial values.Hence, static variables will always have assigned values.


Generating Fibonacci  Numbers The Fibonacci numbers form an interesting sequence in which each number is equal to the sum of the previous two numbers.In other words,

F3 =F2 + F1

Let us write a C program that generates the first n Fibonacci numbers, where n is value specified by the user.


/*program to calculate successive Fibonacci numbers*/


#include < stdio.h >

long int fibonacci  (int count);


main() 
{
int count, n;


printf ("How many Fibonacci numbers?");
scanf ("%d", &n);
printf ("\n");


for (count=1;count <= n;++count)
printf ("\ni= %2d F=%1d", count, fibonacci (count));


}


long int fibonacci (int count)


/*calculate a fibonacci number using the formulas F=1 for n < 3 and F3= F1+F2 for n >=3*/

{
 static long int f1=1, f2=1;
long int f;


f= (count < 3)? 1 : f1+f2;
f2=f1;
f1=f;
return (f);

}

You should understand that f1 and f2 are strictly local variables, even though they retain their values from one function call to another.

The output corresponding to a value of n=6 is shown below.As usual the user's response is underlined.

How many Fibonacci numbers? 6
i=1  F=1

i=2  F=1
i=3  F=2
i=4  F=3
i=5  F=5
i=6  F=8   

No comments:

Post a Comment