Assigned Reading: 17.1
Handouts (available on-line): none
Topics Covered:
Functions can also be given the static designation. In that case, only the functions implemented in that file can use the static function. So, static functions are private functions that cannot be used by functions implemented in a separate file.
When a global variable is given the extern storage class, this indicates that the variable is a global variable declared in a separate file. Changes made to this function would be seen by functions in both files.
If a global variable is declared with the static storage class, then it cannot be used by functions in a separate file using the extern storage class. Again, we can think of static global variables as private global variable that can only be used by functions in that file.
Here's an example with two files: file1.c and file2.c. The variable file_1_var is a global variable declared in file1.c. The main function in file2.c can use this variable by declaring it as an external variable. There is also a variable called private declared in file1.c with the storage class static. The main function in file2.c cannot access this variable directly. It must use the functions set_private and print_private to use this static global variable. Using static global variables gives us better control over how and when global variables are used. Sample run.