当前位置:网站首页>Keywords static, extern + global and local variables

Keywords static, extern + global and local variables

2022-04-23 20:47:00 Computer white

 

 

Catalog

Preface

One 、 A brief introduction to global and local variables

Two 、 keyword + Detailed explanation of global and local variables

1. keyword static+ Modify local variables

2. keyword extern+ Modify global variable

3.static Modify function  

summary


Preface

C All variables in a language have their own scope .C Variables in language , There are two types of... By scope , Local variables and global variables . Before learning global and local variables , First of all, we need to understand the keywords static and extern( Next I will introduce in detail )

Once in the program, it's like a sea , Since then, long hair is a passer-by ......

One 、 A brief introduction to global and local variables

null , stay {} The variables defined inside are local variables , stay {} All variables defined outside are global variables

#include <stdio.h>
int b = 10; //b Defined as a global variable 
int main()
{
	int i = 0;//i Defined as a global variable 
	return 0;
}

Two 、 keyword + Detailed explanation of global and local variables

stay C In language , Keywords are C Language gives us , You can't create !!!

static Is used to modify variables and functions :

1. Modify local variables - Called static local variables
2. Modify global variable - Called static global variables
3. Modify function - Called static function

1. keyword static+ Modify local variables

The following code example :

// Code 1( Without any modification )
#include <stdio.h>
void test()
{
    static int i=0;
    i++;
    printf("%d ", i);
}
int main()
{
 int i = 0;
    for(i=0; i<10; i++)
   {
        test();
   }
    return 0; 
}

Code example after running ( Yes no static Modify and contrast ):

From the above we can see that , When the code has no static When decorating , The result of running is different ...

The first code diagram runs parsing ( nothing static When decorating ): We all know that code execution is compiled and executed from top to bottom , Compile to for loop , That is, enter the custom function test(),test() Is an independent piece of program code , When a piece of program code is executed , Our data will be destroyed , Not in storage , This led to the second visit test() when ,i The value of is still equal to 0; So the running structure of the program will have a loop , Always for 1.

The first code diagram runs parsing ( Yes static When decorating ): When there is static When decorating ,static Modifying a local variable will change the life cycle of the variable ,test() in i The value of is out of order test() This program code will not be destroyed ,i The value of will gradually increase , Static variables are out of scope and still exist , Until the end of the program , The life cycle is over

2. keyword extern+ Modify global variable

The following code example :

// Code 1
//test1.c( Create another source file )
int g_val = 2018;
//test.c
int main()
{
    printf("%d\n", g_val);
    return 0; 
}

// Code 2
//test1.c( Create another source file )
static int g_val = 2018;
//test.c
int main()
{
    printf("%d\n", g_val);
    return 0; 
}

Code example after running ( nothing static modification ):

Code example after running ( Yes static modification ):

  From the above we can see that , When the code has no static When decorating , The result of running is different ...

The first code diagram runs parsing ( nothing static When decorating ): From the above, we can know that a global variable has external link properties , Can be used under different source files , You need keywords when using extern Statement can be used only

  The second code diagram runs parsing ( Yes static When decorating ): A global variable is originally defined by an external link attribute , By static After modification , It becomes an internal link property , Can only be used in their own source file , Cannot be used in other files , In use, the scope becomes smaller , So there will be unresolved external symbols . 

extern yes C Language keywords , Is used to declare external symbols .                                                                 

If a project consists of multiple source files , If you want to refer to an external variable defined in another source file in one source file , You need to use... In the source file of the referenced variable extern Keyword declaration variables .

3.static Modify function  

The following code example :

// Code 1
//add.c
int Add(int x, int y) 
{
    return x+y; 
}
//test.c
extern int Add(int x,int y);// Declare external symbols 
int main()
{
	printf("%d\n", Add(2, 3));
	return 0;
}
// Code 2
//add.c
static int Add(int x, int y) 
{
	return x + y;
}
//test.c
extern int Add(int x,int y);// Declare external symbols 
int main()
{
	printf("%d\n", Add(2, 3));
	return 0;
}

static The principle and of modifying function static Decorating global variables has the same effect , But draw the ladle according to the gourd , There's no show here. Parents can try it .


summary

Global static variables
advantage : Both object methods and class methods can access and modify global static variables , And external classes cannot call static variables , After definition, it will only point to the fixed pointer address , For all objects , Save a space .
shortcoming : The life cycle of existence is long , From definition to the end of the program .
Local static variable
advantage : After definition, only one value will exist , Each call uses the value of the memory address of the same object , Not recreated , Save a space , Can only be used in this local code block .
shortcoming : The life cycle of existence is long , From definition to the end of the program , Can only be used in this local code block .

版权声明
本文为[Computer white]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210546128284.html