English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
A static function in C language is a function whose scope is limited to its target file. This means that the static function is only visible in its target file. A function can be declared as a static function by placing the static keyword before the function name.
The following is an example to prove this point-
There are two files first_file.c and second_file.c. The content of these files is as follows:-
Content of first_file.c
static void staticFunc(void) { printf("Inside the static function staticFunc() "); }
Content of second_file.c
int main() { staticFunc(); return 0; }
Now, if the above code is compiled, an error will be obtained, namely "undefined reference to correct"staticFunc()
"This happens because the functionstaticFunc()
is a static function and is only visible in its target file.
The following program demonstrates a static function in C language:-
#include <stdio.h> static void staticFunc(void) { printf("Inside the static function staticFunc() "); } int main() { staticFunc(); return 0; }
Output Result
The output of the above program is as follows-
Inside the static function staticFunc()
In the above program, the functionstaticFunc()
is a static function that prints "Inside the static function" staticFunc()
"Thismain()
Function CallstaticFunc()
This program can work normally because it can only call static functions from its own target file.