What is a Static Function in C?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Static functions in C are functions that are restricted to the same file in which they are defined. The functions in C are by default global. If we want to limit the scope of the function, we use the keyword static before the function. Doing so, restricts the scope of the function in other files, and the function remains callable only in the file in which it is defined.

Syntax

The syntax of static function in C is as follows:

We use the static keyword along with the return type of the function before the function name to make the function static.

Errors and Exceptions of Static Function in C

Static functions cannot be called from other files since they are restricted to the file in which they are created. Thus if we create a static function, say demo() in one file, and call it from another file, we shall get “Undefined reference to demo() function.” as an error.

Thus they must always be called in the file they are created.

Example of Static Function in C

Let us go through an example of Static function in C.

Input:

Output:

Explanation:

In the above example, we use a function called addition to find the sum of two numbers. We define the addition function as static. As we have defined the function in the same file in which it is being called, we are able to use it to find the sum of two integers inputted by the user.

Few More Examples

In this section, we will go through more examples covering all the cases in which we use Static Function in C.

Example 1: Program to Call a Global Function Inside Another File

In this example, we shall see how a global function is called inside another file.

Let us create two files, first_file.c and second_file.c

In the first file, we create a function called addition which returns the sum of two numbers that are passed to this addition() function.

In the second file, we are writing the main() function, which is calling the addition function from the first file.

first_file.c :

second_file.c :

Input:

Output:

Explanation:

Since addition is a global non-static function, it can be called from the same file or different file. Thus the above program is executed successfully. We take in two numbers from the user and call the addition function to get their sum. The sum is then printed on the screen.

Example 2: Program to call a static function inside Another File

Let us create two files, first_file.c and second_file.c

In the first file, we create a static function called addition to find the sum of two numbers.

In the second file, we are writing the main() function, which is calling the addition function from the first file.

first_file.c :

second_file.c :

Output:

Explanation:

Static functions are restricted to the file in which they are created, and cannot be called from other file. Since we are creating the static function addition() in first_file.c and calling it from second_file.c, compiler is throwing the error "Undefined reference to addition() function."

Example 3: Program to call a Static Function in Same Object File or Function

In this example, we call a static function in the same file, in which it is created.

Output:

Explanation:

We create a static function called demo, which prints "This is a static function" whenever it is called. We call the demo function from main() in the same file. Thus the function is called and we get the desired output to the screen.

Example 4: Program to call Multiple Static Functions in Same File

We are using two functions, one to find the addition of two numbers and another to find the subtraction of two numbers.

Input:

Output:

Explanation:

In the above program, we are creating two static functions addition and subtraction, and calling them in the same file. Thus the program is being executed successfully.

Why the Static Functions are Required?

Sometimes we need to create functions, that should not be visible/sharable/callable to other files i.e., to provide abstraction. E.g. - We might have some internal APIs that we do not want the client to see. Thus, creating a static function, helps us to limit the scope of a function within the same file in which it is created.

We might have different functions with same function name. This may lead to declaration conflict. Instead of using global functions, if we create static functions in each file, then we can use the similar function name in another file. Static function helps us to avoid ambiguity in using same function names in different files.

What is the Use of Static Function in C?

As seen in the above section, the use of static function in C boils down to the following points:

  • It is used to limit the scope of a function in a C program.
  • It is used for reusing the same function name in other files.

Conclusion

  • Functions in C are global by default.
  • To make them local to the file they are created, we use the keyword static before the function.
  • Static functions can't be called from any other file except the file in which it is created.
  • Using static function, we can reuse the same function name in different files.