Home > Article > Backend Development > In C language, static functions
#A static function in C is a function whose scope is limited to its object file. This means that static functions are only visible in their object files. A function can be declared as static by placing the static keyword before its name.
An example demonstrating this is as follows -
There are two files first_file.c and second file.c. The contents of these files are as follows -
Contents of first_file.c
static void staticFunc(void) { printf("Inside the static function staticFunc() "); }
Contents of second_file.c
int main() { staticFunc(); return 0; }
Now, if I compile the above code, I get an error, i.e. " undefined reference to staticFunc()". This happens because the function staticFunc() is a static function and is only visible in its object file.
The program that demonstrates the static function in C is as follows-
#include <stdio.h> static void staticFunc(void){ printf("Inside the static function staticFunc() "); } int main() { staticFunc(); return 0; }
The output of the above program is as follows-
Inside the static function staticFunc()
In the above program, the function staticFunc() is a static function, and it prints "Inside the static function staticFunc()". The main() function calls staticFunc(). The program works fine because the static functions are only called from their own object files.
The above is the detailed content of In C language, static functions. For more information, please follow other related articles on the PHP Chinese website!