C language (static) global variables, (static) local variables summary notes-basic version V1

The road is hindered and long, and the line is coming. Keep your head down and work hard, if you don't talk, you will be a blockbuster! Come on, Sao Nian!

Preface

  When I saw an article about C language global variables, I was deeply moved. After thinking about it, I seem to have a little understanding of these variables, so I have this note.

Reference

  Reference article: In C language, the consequences of global variable abuse are so serious? " Https://mp.weixin.qq.com/s/cijGikfwy4XfRqfvufmUIA "

Basic understanding

  • "Local Variables": Defined inside the function, only valid inside the function; Features: come in to create, go out to destroy;
  • "Static local variable": Defined inside the function, and only valid inside the function; but the memory will only be allocated once at the beginning and will not disappear; Features: come in and create, always keep;
  • "Global Variables": Defined in addition to all functions, the entire project can be used; other files should be used with the extern keyword; and it should be noted that changing the value of a global variable will affect all the same global variables; Features: start to create, Always keep
  • "Static global variables": Defined outside all functions, the same as global variables, but only the defined files can be used; Features: start to create, keep it forever, and use it by yourself;

  Summarized a diagram to help deepen understanding.

Local variable Local static variable Global variable Global static variables
Scope Partial Partial Global Global
Memory space Stack Static storage area Static storage area Static storage area
Lifetime temporary long long long
Summary features Create temporarily, go out and destroy. Created temporarily and kept. Start to create, always keep. Start to create, keep it, and use it by yourself.

Each break

Local variable

  Local variables are variables defined inside the function and are only valid inside the function.

  The simple test code is as follows:

#include <stdio.h>

int main()
{
    
    
	int iTemp = 10;					// 这是一个局部变量
	printf("iTemp = %d\n", iTemp);
}

  The test results of the above code are as follows:

pi@raspberrypi:~/08-Programming/10-test/12-variableTest $ gcc jubu.c -o jubu.out
pi@raspberrypi:~/08-Programming/10-test/12-variableTest $ ./jubu.out 
iTemp = 10

Static local variable

  As the name suggests, it is a local variable is defined within the same function, but is static , that is, storage and life cycle are not the same.

  The simple test code is as follows. In the main function, the sub-function TempAdd() is called 5 times in a loop and printed out.

#include <stdio.h>

int TempAdd()
{
    
    
	static int iTemp = 0;				// 这是一个静态局部变量
	iTemp++;
	return iTemp;
}

int main()
{
    
    
	for(int i = 0; i < 5; i++)
	{
    
    
		printf("Temp = %d\n", TempAdd());
	}
}

  You can guess the results of the program, as follows:

pi@raspberrypi:~/08-Programming/10-test/12-variableTest $ gcc jubujingtai.c -o jubujingtai.out
pi@raspberrypi:~/08-Programming/10-test/12-variableTest $ ./jubujingtai.out 
Temp = 1
Temp = 2
Temp = 3
Temp = 4
Temp = 5

  It can be seen from the above running results that when the sub-function is called for the first time in the main function, the static local variable iTemp is created . After each call, the initial value will not be re-assigned, but the original value will be used directly.

  Reflects the principle: temporary creation, always retained;

Global variable

  Global variables, variables defined outside of all functions, are valid for the entire project file. The current file can be used directly. If it is not the current file, the keyword extern must be added at the beginning of the calling file .

  The simple test code is as follows:

#include <stdio.h>

int iTemp = 0;			// 这是一个全局变量

int main()
{
    
    
	for(int i = 0; i < 5; i++)
	{
    
    
		printf("iTemp = %d\n", iTemp++);
	}
}

  The test results are as follows:

pi@raspberrypi:~/08-Programming/10-test/12-variableTest/13-quanju $ gcc quanju.c -o quanju.out
pi@raspberrypi:~/08-Programming/10-test/12-variableTest/13-quanju $ ./quanju.out 
iTemp = 0
iTemp = 1
iTemp = 2
iTemp = 3
iTemp = 4

  In order to more clearly reflect that it is a global variable, I will write another sub-function call, the code is as follows:

#include <stdio.h>

int iTemp = 0;			// 这是一个全局变量

void TempAdd()
{
    
    
	iTemp++;
}

int main()
{
    
    
	for(int i = 0; i < 5; i++)
	{
    
    
		printf("iTemp = %d\n", iTemp++);
	}

	printf("----------This is a Dividing line!---------\n");

	TempAdd();

	printf("iTemp = %d\n", iTemp);
}

  The result of the operation is as follows. You can think about why the value of iTemp last time is 6?

pi@raspberrypi:~/08-Programming/10-test/12-variableTest/13-quanju $ gcc quanjuV2.c -o quanjuV2.out
pi@raspberrypi:~/08-Programming/10-test/12-variableTest/13-quanju $ ./quanjuV2.out
iTemp = 0
iTemp = 1
iTemp = 2
iTemp = 3
iTemp = 4
----------This is a Dividing line!---------
iTemp = 6

Static global variables

  Static global variable is a global variable, but coupled with static then it defines the scope of this variable.

  In order to test and verify the static global variables, I did a simple experiment, but the results were not satisfactory, but I should first organize my thoughts. In the follow-up learning process, I need to deepen my understanding again.

  I define three files: main.c, , static.c, static.hwhere the static global variables are defined in static.h, the specific code as follows:

// main.c
#include <stdio.h>
#include "static.h"

int main()
{
    
    
	iTemp = 10;
	printf("iTemp = %d\n", iTemp);
}
// static.c
#include <stdio.h>
#include "static.h"

void TempTest()
{
    
    
	for(int i = 0; i < 5; i++)
	{
    
    
		iTemp++;
		printf("iTemp = %d\n", iTemp);
	}
}
// static.h
#ifndef _STATIC_H_
#define _STATIC_H_

static int iTemp = 0;		// 这是一个静态全局变量

#endif

  The above code, the results of compiling and running are as follows:

pi@raspberrypi:~/08-Programming/10-test/12-variableTest/14-quanjujingtai $ gcc main.c static.c static.h -o a.out
pi@raspberrypi:~/08-Programming/10-test/12-variableTest/14-quanjujingtai $ ./a.out 
iTemp = 10

  As for why there is such a result, I thought about it, there may be the following reasons:

  • The main function contains the definition of static global variable header file;
  • This code structure is not standardized, not up to test static global variable standards;
  • Other possible reasons have not been thought of yet;

to sum up

  1. Just have a basic understanding of these four variable types, and we will continue to in-depth follow-up;
  2. In the process of summarizing, I found some other more in-depth information, but I think the better way is: at one stage, summarize and output the things of one stage; then move to the next stage, and then summarize; wait until several stages are over , Review and summarize the knowledge of the several stages that have been learned, and output a more in-depth note summary.
  3. In the process of learning, you must let yourself think more. You can't say what you have learned. It will be over after a glance. If you don't think by yourself, you won't form your own opinions, and you won't be integrated into your knowledge system!

If the content of the article is wrong, please comment / private message a lot of advice, thank you! If you think the content of the article is not bad, remember to click three links (like, bookmark, leave a message), your support is my greatest encouragement, thank you!

Guess you like

Origin blog.csdn.net/Fighting_Boom/article/details/108418131
Recommended