Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement static modifier function in c language

Shulou Source: shulou.com Published: 2022-06-03 08:00:38 09月20日 Update

This article mainly talks about "how to implement the static modification function in c language". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to implement the static modification function in c language.

1. A static function can only be seen in the file in which it is declared, and other files cannot refer to the function.

2. Different files can use static functions with the same name and do not affect each other.

3. Functions declared with static cannot be referenced by another file.

Instance / * file1.c * / # include static void fun (void) {printf ("hello from fun.\ n");} int main (void) {fun (); fun1 (); return 0;} / * file2.c * / # include static void fun1 (void) {printf ("hello from static fun1.\ n");} / * output: error:file1.c: (.text + 0x20): undefined reference to 'fun1' collect2: error: ld returned 1 exit status*/

Expansion of knowledge points:

In C language, the literal meaning of static can easily lead us astray. In fact, it has three functions.

The first and most important item to introduce it: hiding.

When we compile multiple files at the same time, all global variables and functions without the static prefix have global visibility. In order to understand this sentence, I will give an example to illustrate. We need to compile two source files at the same time, one is A.C and the other is main.c.

The following is the content of A.C.

Char a = 'Hello; / / global variablevoid msg () {printf ("Hello\ n");}

The following is the content of main.c

Int main (void) {extern char a; / / extern variable must be declared before use printf ("% c", a); (void) msg (); return 0;}

The running result of the program is:

A Hello

At this point, I believe you have a deeper understanding of "how to implement the static modification function in the c language". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Function file language content global simultaneous static a.c learning compiling different practical deeper same important two for example function interest prefix Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Information Shulou Tech Info macOS Shulou Technology