Javatpoint Logo
Javatpoint Logo

Static function in C

In this topic, we will discuss the static function in the C programming language. By default, every function is declared as global that can be accessed anywhere inside a program. The static keyword is used before the function's name to make any function as the static function. And the scope of the static function in C is limited to its object files or only a program. It means that the static function is only visible to its program files and restricts other files or programs from accessing the static function.

Static function in C

Syntax

Here, we use a static keyword before the function_name to make the given function th static.

Some important points of static function

  1. A static keyword can be used before the name of a function to make it static.
  2. However, the scope of the static function is throughout the program only. It means the static function can be called inside the same program or file.
  3. If we try to access the static function from another file, it throws an error.
  4. We can make a global function as the static by adding the static before the function name of a program.

Program to call a global function inside another file

In this program, we need to create two files, file1.c and file2.c. Now, we create a global function inside the file1.c, as shown below.

File1.c

After defining the global function in the file1.c file, now we create another file file2.c and then call the global function add() in it.

File2.c

Output

Enter any two numbers: 5
20
 The sum of the two numbers is: 25

As we can see in the above program, we create two files, file1.c and file2.c. In file1.c, we define a global function add() that is called inside the file2.c. And in file2.c, file1.c will be treated as the reference file that contains the definition of the global function and is called inside the main() function to return the sum of two integer numbers.

Program to call a static function inside another file

Let's create a program to make a global function as a static function by adding the static keyword before the function and then call in file2.c.

File1.c

Now we create another file with name file2.c and call the static function to check its scope.

File2.c

Output

Undefined reference to myFun() function.

When we compiled the above program, it throws an error as "undefined reference to myFun(). Because of the static function myFun(), its object files can only access, and it does not allow other functions to access the static function.

Program to call a static function in same object file or function

Let's create a program to demonstrate the use of the static function in C.

File1.c

Output

Enter an integer number: 25
The square of the 25 is 625

In the above program, we create a static function inside the same object file File1.c. In File1.c, the main() function calls the static function mySqr() to return the square of the given number. The static function works correctly as the scope of the static function is only within its object file or program.

Program to call multiple static function in same file

Let's create a program to create multiple static function and call inside the main() function.

Program.c

Output

Enter an integer number: 36
 The square of the 36 is 1296
 I am inside the static function.
 The square root of the 36 is 6

In the above program, we create multiple static functions in the same object file, and each static function is called inside the main() function to return the value.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA