C static functions are limited to the file where they're defined. Unlike global functions, their scope is restricted. Simply use the 'static' keyword before the function to define its scope.
In addition, the scope of a static function in C is confined to its object files or to a program. It signifies that the static function is exclusively visible to its program files and that other files or programs cannot access the static function.
In this article, we would be discussing static functions in C in detail, along with examples.
What is a Static Function in C?
In C, a static function is a function with limited visibility that can only be accessed from the source file in which it was defined. It can neither be called from nor accessed from other source files. In sizable codebases, this encapsulation helps avoid naming conflicts. The "static" keyword is used to declare static functions before their return type and name. They are frequently used for internal implementation details or utility functions that shouldn't be visible to other program components. Static functions improve code modularity and maintainability by keeping their scope to a single source file, preventing unauthorized external usage.
Some Important Points of Static Functions
To make a function static, add the static keyword before its name.
The static function's scope is limited to the whole program. It indicates that the static function can be used inside the same program or file.
It throws an error if we try to call the static method from another file.
By putting the static before the function name of a program, we may make a global function static.
Syntax
By default, functions in C are global. The term "static" before the name of a function makes it static.
The function func(), for example, is static.
static int func(void)
{
printf("This is a static function ");
}
Unlike global functions in C, Static functions are only accessible from the file where they are declared. As a result, we make functions static when we want to limit access to them. Another reason to make functions static is to avoid reusing the same function name across many files.
For example, if the following program is stored in a single file named file_one.c,
static void func_one(void)
{
puts("func_one is called");
}
Save the following program in a separate file called file_two.c.
int main(void)
{
func_one();
getchar();
return 0;
}
When we run "gcc file_two.c file_one.c" on the above code, we receive the error "undefined reference to 'func_one'." This is because in file_one.c, func_ one() is marked static and cannot be used in file_two.c.
Example of Static Function in C
Let's now look at a standard example involving static functions in C language:
C
C
#include <stdio.h> void normalAddition(int i,int j) { printf("This is a normal function.\n"); printf(i+j); } static void staticAddition(int i,int j) { printf("This is a static function.\n"); . printf(i+j); } int main() { normalAddition(3,5); staticAddition(2,5);
return 0; }
Output
This is a normal function
8
This is a static function
7
Explanation
Here we wrote two functions. One is normalAddition which is a global function that can be used in other files too, and we wrote a static function called staticAddition which cant be used in another file.But the static function has the scope of the same file or object where it is initialized, so we can use it in this file, so when we call, it executes successfully.
Few More Examples
Let's take a few more use cases where we can understand the usage of static functions in more depth:
Example 1: Program to Call a Global Function Inside Another File
Let's look at an example where we call a global function from another file into the current file.
first.c
C
C
#include <stdio.h> int addition(int i,int j) { return i+j; }
second.c
C
C
#include <stdio.h> int addition(int i,int j); int main(){ int sum = addition(15, 35); printf("Sum is : %d", sum); }
Output
Sum is : 50
Now when your run the command gcc first.c second.c , the compiler goes line by line and sees a declaration for the addition function, which it picks from the second.c file(because it is a globally declared function) and uses it for calculating the sum of the 15 and 35 and prints out 50; this shows that we successfully used the global function of another file in our file.
Example 2: Program to call a static function inside Another File
Let's look at a code example where we try to call the static function from another file into our main file
#include <stdio.h> int addition(int i,int j); int main(){ int sum = addition(21, 9); printf("Sum is : %d", sum); }
Output
Undefined reference to addition()
This example is the same as example one, but here instead of making the addition function a global function, we make it a static function, due to which its scope is limited to the first file, and it cannot be accessed out of this file, so when we try to call this addition function in the second file to calculate the sum of 21 and 9 it gives an error for undefined reference because it couldn't access the addition function of first file due it being a static function.
Example 3: Program to call a Static Function in the Same Object File or Function
Here's an example to show that we can call a static function inside the same file/object where we declared it:
C
C
#include <stdio.h> static void printStatic(){ printf("Static function was called inside the file"); } int main(){ printStatic(); }
Output
Static function was called inside the file
Because the static function has the scope of the file or object where it is declared so we can access the printStatic function inside the file, we get the output because the static can be accessed within this file.
Example 4: Program to call Multiple Static Functions in the Same File
We can access all the static functions inside the file, where we declare them; let's see how:
C
C
#include <stdio.h> static int addition(int num1, int num2){ return (num1 + num2); } static int multiplication(int num1, int num2){ return (num1*num2); } int main() {
int sum = addition(23, 17); int diff = multiplication(12, 4); printf("Sum: %d", sum); printf(" Multiplication: %d", diff); return 0; }
Output
Sum:40 Multiplication:48
Here we were able to access both the static functions inside the file. This shows we can access all the static functions if we have declared them in the same file.
Errors and Exceptions of Static Function in C
In C, static functions are subject to certain errors and exceptions:
Scope Restriction: Static functions are restricted to file scope, meaning they can only be accessed within the file in which they are defined. Attempting to call a static function from another file will result in a compiler error.
Linkage Limitation: Static functions have internal linkage, which means they cannot be accessed by functions in other translation units. This can lead to linker errors if there are attempts to access the static function from outside its defining file.
Initialization: Static functions are not initialized automatically like global variables. They need to be declared before use, otherwise, the compiler will issue an error.
Memory Management: While static functions do not contribute to the size of the executable directly, they may contribute to memory usage during runtime if they allocate static variables or use large data structures within the function.
Debugging: Debugging static functions may be more challenging compared to non-static functions since they cannot be easily accessed from outside the file. This can make it harder to inspect their behavior and identify issues.
Why are Static Functions Required?
Static functions are very useful and come in handy in many cases let's look at them:
The static function and keyword in C is useful when it comes to object-oriented programming, more importantly, it is used to enforce encapsulation, which means it is encapsulated in one place and cannot be edited, accessed, or changed at another external place.
This also helps allow the same function name to be used in other files because the static function cannot affect the code in another file even tho they have the same name.
Static functions can also help enforce more security because of their limited scope of access
Static functions help reduce naming conflicts in bigger codebases, because they help with multiple functions to have the same name, without affecting each other.
Uses of Static Function in C
The use of static functions in C comes down to the following factors, as can be seen in the section above:
It is used to restrict the scope of a function: In a C source file, there are often internal and public APIs, with the latter being only visible to clients (like main()). Static functions are used in C programming to limit client access to internal APIs.
It's employed to reuse the same function name across multiple files: Static functions are also utilized in C language programming to eliminate ambiguity if two source files in a program share the same function name.
Frequently Asked Questions
What is the static function in C?
A static function in C is a function with file scope, meaning it can only be accessed within the file where it's defined.
Why use static function?
Static functions encapsulate functionality within a file, reducing namespace pollution and preventing external access, enhancing code modularity and security.
What is static in C++?
In C++, static denotes class-level variables or functions, shared among all instances of the class rather than specific to individual objects.
What is static void in C?
In C, static void denotes a function returning void with file scope, limiting its visibility to the current file.
What is difference between static function and normal function?
The key difference between static and normal functions in C is scope. Static functions are limited to file scope, while normal functions can be accessed globally.
Conclusion
In this article, we have extensively discussed the static functions in C with the help of examples. We learned to define a static function and also learned about its scope.
We hope this blog has helped you enhance your knowledge of C's static functions. If you want to learn more about C, check out our articles, C Archives.