Home > Backend Development > C++ > body text

In C language, static functions

王林
Release: 2023-09-17 10:57:03
forward
1044 people have browsed it

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() ");
}
Copy after login

Contents of second_file.c

int main()
{
   staticFunc();
   return 0;
}
Copy after login

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-

Example

#include <stdio.h>

static void staticFunc(void){
   printf("Inside the static function staticFunc() ");
}

int main()
{
   staticFunc();
   return 0;
}
Copy after login

Output

The output of the above program is as follows-

Inside the static function staticFunc()
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template