Previous: 14.1.6 Static Variables
Up: 14.1 Storage Classes
Next: 14.1.8 Stack vs Heap Allocation
Previous Page: 14.1.6 Static Variables
Next Page: 14.1.8 Stack vs Heap Allocation
Like variables, functions in C have a storage class and scope. All functions in C are external by default and are accessible to all source files. However, functions may be declared to be of static class, in which case they are accessible only to functions in the file in which they are defined, not to functions in other files. This is another way of hiding information. Information hiding makes these static function names invisible to all other files; thus, these names may be used to define other functions elsewhere.
Here is an example that uses static variables as well as a static function. The program assigns bins to different part numbers. The array, index, represents the bin number where the part number is stored (it is easy to generalize the program to structures). The program is organized in two files, bins.c and binutil.c. The first file, bins.c, contains the driver which reads in the part numbers, and calls a function, getbin(), to assign a bin number to each part number. Finally, the driver prints the bins and the corresponding part numbers using the function printbin(). Here are the prototypes:
/* File: binutil.h */ void getbin(int bin, int part, int lim); void printbin(int bin, int lim);
The function getbin() needs three arguments: an array of bins, a part number, and the array size limit. The bin number is just the array index, so getbin() assigns one of the bins in the array to the part number, and stores the part number in the array at the corresponding bin number index. The function printbin() needs the array of bins and its size as arguments. It prints out each bin number index and the corresponding part number stored at that array index. The driver is shown in Figure 14.11.
The program loop reads a part number and if it is not zero, it calls getbin() to assign a bin number to the part number. If the part number is zero, the loop terminates, and printbin() prints bin numbers and corresponding part numbers.
Let us now implement getbin(). Unused array elements of bin should be initialized to some invalid part number, say -1, so that printbin() would be able to distinguish the valid elements of the array. The first time getbin() is called, it calls initbin() which initializes bin to -1. In addition, getbin() should assign the next available index to the part number. The functions are shown in Figure 14.12.
The function getbin() uses a static variable, first, initialized to TRUE, to determine if the function is being called for the first time. When the function is called the first time, it initializes the array, bin and changes first to FALSE. A second static variable, bin_number is used to remember the next available bin number between function calls. As a bin is assigned to a part number, bin_number is incremented. Since it is a static variable, its latest value is available each time the function is called. The function printbin() merely prints each array index and the part number stored at that index. Initialization of the array bin is done by a static function initbin(). This function is not required anywhere else, and so a static class is declared for it, thus the details of array initialization are hidden from all other functions. A sample run of the program is shown below: