What is a Static Function in C?

quiz
Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

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:

static returnType functionName(arguments)
{
    //Here goes the function body
}

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.

#include <stdio.h>

//static function
static int addition(int num1, int num2){
    return (num1 + num2);
}

int main()
{
    int num1, num2;
    printf("Enter two nos: ");
    scanf("%d %d", &num1, &num2);
    int sum = addition(num1, num2);
    printf("Sum is : %d", sum);
    return 0;
}

Input:

Enter two nos: 
5 9

Output:

Sum is 14

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 :

//global function
int addition(int num1, int num2){
    return (num1 + num2);
}

second_file.c :

#include <stdio.h>
int add(int num1, int num2); 
int main()
{
    int num1, num2;
    printf("Enter two nos: ");
    scanf("%d %d", &num1, &num2);
    int sum = addition(num1, num2);
    printf("Sum is : %d", sum);
    return 0;
}

Input:

Enter two nos: 
5 9

Output:

Sum is 14

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 :

//static function
static int addition(int num1, int num2){
    return (num1 + num2);
}

second_file.c :

#include <stdio.h>  

int main()
{
    int num1, num2;
    printf("Enter two nos: ");
    scanf("%d %d", &num1, &num2);
    int sum = addition(num1, num2);
    printf("Sum is : %d", sum);
    return 0;
}

Output:

Undefined reference to addition() function.

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.

#include <stdio.h>

//static function
static void demo(void) 
{  
    printf ("This is a static function");   
}  


int main()
{
    demo();
    return 0;
}

Output:

This is a static function

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.

#include <stdio.h>

//static function1
static int addition(int num1, int num2){
    return (num1 + num2);
}

//static function2
static int subtraction(int num1, int num2){
    return (num1 - num2);
}

int main()
{
    int num1, num2;
    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);
    int sum = addition(num1, num2);
    int diff = subtraction(num1, num2);
    printf("Sum is: %d", sum);
    printf(" and difference is: %d", diff);
    return 0;
}

Input:

Enter two numbers:
5 4

Output:

Sum is: 9 and difference is: 1

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.
Free Courses by top Scaler instructors