C Advanced Static Usage

Static usage

Example 1

#include <stdio.h>

#include <unistd.h>

 

int fun(void);

 

int main(int argc, const char *argv[])

{

       while(1)

       {

              fun();

              sleep(1);

       }

 

       return0;

}

 

int fun(void)

{

       intnum = 1;

 

       num++;

       printf("num= %d\n",num);

 

       return0;

}

Let's take a look at the program output:

num = 2

num = 2

num = 2

If we change the fun function to the following:

 

int fun(void)

{

       staticint num = 1;

 

       num++;

       printf("num= %d\n",num);

 

       return0;

}

Let's take a look at the program output:

 

num = 2

num = 3

num = 4

Why does the value of num keep increasing after num is statically modified?

This is the first usage of static we are talking about: static modifies local variables

When a local variable is modified by static, the variable is called a static local variable. The storage space of the static local variable is in the static storage area, not in the same area as the stack area of ​​the function, so the static local variable will not be released after the function is finished running. Static local variables are only assigned an initial value once. The next time the static local variable is used, the value saved last time is directly used without reassignment. Therefore, when num is statically modified, num is only assigned once, and num will not be released after the fun function is called by main. After main calls fun next time, the initial value of num is the value after the exit of fun last time.

 

 

Example 2

There is a project with two c files, one is the hello.c file and the other is the main.c file.

in the hello.c file

#include <stdio.h>

 

int hello(void)

{

       printf("helloworld!\n");

 

       return0;

}

 

int hello1(void)

{

       hello();

 

       return0;

}

in main.c file

#include <stdio.h>

int hello(void);

int hello1(void);

 

int main(int argc, const char *argv[])

{

       hello1();

       hello();

 

       return0;

}

Let's take a look at the program output:

 

hello world!

hello world!

 

If we are in the hello.c file, add static before the hello function

staic int hello(void)

{

       printf("helloworld!\n");

 

       return0;

}

 

Let's take a look at the compilation

The following error will appear in mian.c:

main.c:(.text+0xc): undefined reference to`hello'

collect2: error: ld returned 1 exit status

The above error says that the definition of hello cannot be found in main.c.

If in the main.c file, remove the hello() function, as follows

int main(int argc, const char *argv[])

{

       hello1();

 

       return0;

}

Let's take a look at the compilation, there are no errors, and look at the execution results:

hello world!

 

Why can't the main.c file call the function in the hello.c file after adding static before the hello function?

 

This is the second usage of staic we are talking about: staic modification function

When staic modifies a function, the function can only be called by other functions of this file and not allowed to be called by functions of other files, that is, the private function of the file, only the file has exclusive rights, such a function is called "internal function" ".

 

Example 3

There is a project with two c files, one is the hello.c file and the other is the main.c file.

in the hello.c file

 

#include <stdio.h>

 

int global = 12345;

 

int hello(void)

{

       printf("Thisis hello fun! global = %d\n",global);

 

       return0;

}

in main.c

#include <stdio.h>

int hello(void);

extern global;//Declare the variable defined in other files

 

int main(int argc, const char *argv[])

{

       printf("Thisis main fun! global = %d\n",global);

       hello();

 

       return0;

}

 

Let's take a look at the output:

This is main fun! global = 12345

This is hello fun! global = 12345

 

If in the hello.c file, add static before defining the variable global, the procedure is as follows:

#include <stdio.h>

 

static int global = 12345;

 

int hello(void)

{

       printf("Thisis hello fun! global = %d\n",global);

 

       return0;

}

 

Let's take a look at the compilation:

The following error appears in the mian.c file:

main.c:(.text+0xa): undefined reference to`global'

collect2: error: ld returned 1 exit status

Could not find global definition in mian.c.

If you change the mian.c file to the following:

#include <stdio.h>

int hello(void);

 

int main(int argc, const char *argv[])

{

       hello();

 

       return0;

}

Let's take a look at the output:

This is hello fun! global = 12345

 

Why can't the main.c file use the global variable global of hello.c after adding static before the global variable global?

This is the third usage of staic we are talking about: staic modifies global variables

After staic modifies a global variable, the variable can only be used in this file and is not allowed to be used in other files. This global variable is called a static global variable, and you can treat it as a private variable in this file.

 

Summarize

Three commonly used uses of Static:

1. Modify local variables

When a local variable is modified by static, the variable is called a static local variable. The storage space of the static local variable is in the static storage area, not in the same area as the stack area of ​​the function, so the static local variable will not be released after the function is finished running. Static local variables are only assigned an initial value once. The next time the static local variable is used, the value saved last time is directly used without reassignment. .

 

2. When staic modifies a function, the function can only be called by other functions of this file and not allowed to be called by functions of other files, that is, the private function of the file, only the file has exclusive rights, such a function is called "" inner function".

 

3. After staic modifies a global variable, the variable can only be used in this file and is not allowed to be used in other files. This global variable is called a static global variable, and you can treat it as a private variable in this file.

 

The following gcc commands need to be used in this blog. How to use the embedded Linux compiler GCC is what I will talk about in my next blog.

gcc -c hello.c -o hello.o

gcc -c main.c -o main.o

gcc main.o hello.o -o main

 

 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325421920&siteId=291194637