C Language | Structure Declarations | Storage Classes for Functions

Function declarations can use the extern and static specifiers to define storage classes. To hide a function so that other source files cannot call it, declare the function with static.

Function Visibility

Unlike variables, functions do not have an inner scope level. A function has global duration and can be referenced anywhere in the same file. As explained in the article about function declarations, a prototype declaration is required before calling a function.

When compiling multiple files together, function names can conflict just like variable names. Functions therefore also have storage classes. A function can use either static or extern.

A function with the static storage class can be called only from the file where it is defined. Functions in other files cannot call it. In contrast, an extern function can be called from other files. If you omit the storage class, extern is implied.

Code 1

#include <stdio.h>

void Function1(void);
void Function2(void);

int main() {
 /*Function1();  /*error*/
 Function2();
  return 0;
}

Code 2

static void Function1() {}
extern void Function2() {}

If you compile Code 1 and Code 2 together, main() can call Function2() but cannot call Function1() because it is not visible from Code 1. Uncommenting the Function1() call causes an error. The static storage class hides Function1() from external files.

This is useful for a function specialized for a particular process in one file. Assign the static storage class to a function with low independence to avoid name conflicts.