In C, all the variables have data types and storage classes. The storage classes determine the lifetime of the storage associated with the variable. The following variable storage classes are most relevant to functions:
The viables are categorized depending on the place of their declaration as INTERNAL (local) or EX TERNAL (Global) It is important to understand the concept of storage classes and their utility in order to develop multifunction programs.
Automatic variables
Variables defined inside a function are called automatic local variables since they are automatically created each time the function is called, and since their values are local to the function. It implies that the value of the local variable can only be accessed by the function in which it is defined. Its value cannot be accessed by any function.
When defining a local variable inside a function, it is more precise in C to use the keyword auto before the definition of the variable. The following statement is an example for local variable declaration.
auto int numbr;
Since the C compiler treats any variable declared inside a function as an automatic local variable, it is not necessary to specify the keyword auto along with variable declaration.
main()
{
int number;
}
One important feature of automatic variables is that their value cannot be changed accidentally. This assures that we may use the same variable name in different functions in the same program without causing any confusion to the compiler. The following example illustrates the working of automatic variables.
/* This program illustrates the working of automatic variables */
Example 5.9
#include <stdio.h>
int main ()
{
int test_variable = 1000;
function2 ();
printf ("%d \n", test_variable);
}
function1()
{
int test_variable=10;
printf ("%d\n", test_variable);
}
function2()
{
int test_variable=100;
function1 ();
print f ("%d\n", test_variable);
}
The output of the program will be as follows
10
100
1000
In the above program, function2) is called from main( ). When the control goes to function2(), function 1() is called where the value of the test_variable is assigned as 10. Then, the next statement in function2() is executed which prints the value of the test_variable(now, it is 100) that is assigned in function2(). After executing this statement, the control returns to the main) function. where the third statement is executed i.e. the value of the variable in the main() function (1000) is printed.
External variables
Variables that are both alive and active throughout the entire program are known as external variables. They are otherwise called global variables. Unlike local variables, global variables can be accessed by any function in the program. The striking feature of global variables is that they are declared outside any function. This indicates their global nature i.e. they do not belong to any particular function. Any function in the program can then access the value of that variable and can also change its value if desired.
The working of global variables could be understood in a better way, with the aid of the following example.
Example 5.10
#include <stdio.h>
/*This program illustrate the working of global variables*/
int test_variable; /* global variable declaration */
int main()
{
test_variable = 10;
function1();
function2();
function3();
printf("%d\n", test_variable);
}
function1()
{
test_variable = test_variable + 10;
printf("%d\n", test_variable);
}
function2()
{
int test_variable;
test_variable = 1000;
printf("%d\n", test_variable);
}
function3()
{
test_variable = test_variable + 10;
printf("%d\n", test_variable);
}
The output of the above program will be as follows
20
1000
30
30
In the above program, one thing to be noted is that the variable test variable is used in all functions. but none except function2(), has a definition for test_variable. Because test_variable is declared above all functions, it is available to each function and there is no need to pass this variable as a function argument. Once a variable has been declared global, any function can use it and change its value. Then, subsequent functions can reference only that new value.
Static variables
The word static, in a generic manner, refers to the notion of anything that is inert to changes. Likewise. the value of a static variable in C persists until the end of the program. This is the striking feature of static variable --it does not come and go as the function is called and returns. A variable can be declared static using the keyword static like
static int number;
static float sqrt;
A static variable may be an internal type or external type depending on the place of declaration. The following program illustrates the usage of static variables.
Example 5.11
#include <stdio.h>
/* Program to illustrate static variables */
int main()
{
int count;
for (count = 1: count <= 3; count++)
stat_func ();
}
stat_func()
{
static int stat_ var =0;
stat_var = stat_var * 1;
print f( "%d\n", stat_var) ;
}
The output will be as given here under
A static variable is initialized only once and when the program is compiled, it is never initialized again. During the first call to stat_func(), the value of stat_var is incremented to
Since stat_var is a static variable, this value persists and therefore the next call adds another 1 to it, giving it a value of 2. The value of the variable becomes three when the third call is made. If the variable had been declared as a little variable, the output would have been as follow
This is because each time the function stat_func() is called, the local variable is initialized to zero. When the function terminates, the value of 1 is lost.
An external static variable is declared outside of all functions and is available to all the functions in that program. The difference between a static external variable and a simple external variable is that the static external variable is available only within the file where it is defined while the simple external variable can be accessed by other files.
Recursive functions
The C language supports a feature called recursion Recursive functions can be effectively used in applications in which the solution to a problem can be expressed in terms of successively applying the same solution to subsets of the problem A useful example of recursion is the evaluation of the factorial of a given number.
Example 5.12
#include <stdio.h>
/* Function to display the factorial of numbers till 10 */
factorial (n)
int n;
{
int fact;
if (n== 0)
return (1) ;
else
fact = n * factorial (n-1);
return(fact);
}
int main()
{
int j;
for (j= 0; j < 10; j++)
printf ( "%d\n", factorial(j));
}
Functions with Arrays
Like the values of simple variables. it is also possible to pass the values of an array to a function. To pass an array to a called function, it is sufficient to list the name of the array without any subscripts, and the size of the array as arguments. For example, the call to the function great (arr_var, n) will pass all the elements contained in the array arr_var of size n. The called function expecting this call must be appropriately defined. The largest function header will look like
float great arr_var, n ();
float arr_var[];
int size;
The function great is defined to take two arguments -- the array name and the size of the array to specify the number of elements in the array. The declaration of the formal argument array is made as follows:
float arr_var[];
The pair of brackets inform the compiler that the argument arr_var is an array of numbers. It is not necessary to specify the size of the array here. An example for functions with arrays is given below.
Example 5.13
In this example, an array is passed as an argument to the function, sort (), and the function performs the sorting operation on the elements of the array.
#include <stdio.h>
int main()
{
int j:
static int mark (5] = {41,78,56,79,45};
printf ("The marks before sorting \n");
for (j=0; j < 5; j++)
{
printf("%d\t", mark [j]);
printf("\n\n\n");
sort (5,mark) ;
}
sort (num, arr)
int num, arr[];
{
int i,j,t;
for(i=0; i < num ; i++)
for(j=i+1; j < num; j++)
if(arr[i] > arr[j])
{
t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
printf("The marks after sorting /n");
for(i=0; i<5; i++)
print("%d\t", arr[i]);
}