preface : Good luck to all programmers 1024 happy , Technology is getting better and better , I also hope that today next year can be proud to compare with today :“ I've become great !”, Don't say much , Today, I will reward myself for writing an article ^_^

stay c In language :
static Is used to modify variables and functions

1. Modify local variables - It is called static local variable

2. Modify global variables - It is called static global variable

3. Modifier function - Called static function

one , Modify local variables

Here we start with the code
#include <stdio.h> void y() { int i = 1; i++; printf("%d ",i); } int main() {
int a = 0; for (a = 0; a < 10; a++) { y(); } return 0; }

Print here 10 individual 2, reason : When the function is output for the first time ,i The stored data is released , That is, the second time you enter the function ,i Recreate will still be equal to 1. And so on, all will be printed 2.

Then let's add it before it static What changes will happen .
#include <stdio.h> void y() { static int i = 1; i++; printf("%d ",i); } int
main() { int a = 0; for (a = 0; a <10; a++) { y(); } return 0; }

The result here is 2 ~ 11, use static Modify local variables i after , Make it a static local variable , That's what happened y After function i The stored data has not been released ,  On the second entry i
Just for 2 Yes , after i++ After printing, it becomes 3, Last print 2~11.

There is a stack area in memory , Heap area , Static area . Local variables are stored in the stack area , Global and static variables are stored in the static area .

use static Modify local variables , In fact, it changes the storage type of variables , Change it from stack storage to static storage
, Make the static local variable out of the scope and not be released , The life cycle also becomes the same as the global variable , They are released after the program is completed . So the above static local variables i
It is not destroyed after being out of scope , Instead, it is stored in a static area . It should be noted here that even if i Change to static local variable , But its scope has not changed , Only in function y Internal use .

two , Modify global variables

Here we can see that two source files have been created ,( It is necessary to add global variables to another file. extern + type + name  , The blogger here also mentioned earlier
), The printed value is 20. Then let's add static What will it become

The compiler reports an error here ,
This is because a global variable has an external link attribute in the internal file of the whole project , use static When decorating global variables , Make it a static global variable , It also makes the external link attribute of the global variable become the internal link attribute , This global variable can only be used under its own source file .
It can be understood that public toilets become private toilets , Lost contact with the outside world ( ha-ha ). In this way, the global variable cannot be used in another file , Of course, global variables age You can still use it in your own files , Examples are as follows :

three , Modifier function

actually static Decorating functions is similar to decorating global variables

Here we can see that the function can be used normally , Then when we were test2.c Add after middle function static And

You can see that the error is reported here , The principle here is the same as that of global variables , The function itself has external link properties , When used static When decorating, make its external link attribute become internal link attribute
, So that the function Add Can only be used in your own source file , It cannot be used by other internal source files of the project . 

OK, today's introduction static That's it , If you find mistakes and deficiencies, please point them out , Now thank you .

Technology
©2019-2020 Toolsou All rights reserved,
evo Tool usage problems ——Degenerate covariance rank, Umeyama alignment is not possible Experiment 4 Automated test tools - software test mysql Export data sql sentence _mysql according to sql Query statement export data Create a thread ——— Javaweb (3) Data structure experiment ( three )—— Stacks and queues TS stay vue2 Writing in the project web Front end signature plug-in _signature_pad Plug in implements electronic signature function docker Where is the image stored Qt Getting Started tutorial 【 Basic controls 】QCalendarWidget calendar control springboot How to get reality in ip address