​Static variables and dynamic variables

angryTom
Release: 2019-10-24 09:17:00
Original
4631 people have browsed it

​Static variables and dynamic variables

Static variables and dynamic variables

Static variables

is a variable modified with static when it is defined, in the form of

static TYPE var_name = init_value;
Copy after login

Dynamic variable, in the form of

TYPE var_name = init_value;
Copy after login

That is, there is no static modification. The =init_value can be omitted.

Distinguish between global variables defined outside the function and local variables within the function, scope, lifecycle, and when there is no explicit initialization The initial value is different.

1 Dynamic global variables:

The scope is the entire project, that is, dynamic global variables can be used in all files that are eventually compiled into executable files.

The life cycle is from the program running to the program exit, that is, throughout the entire running time.

The default initialization value is 0 when there is no explicit initialization.

2 Static global variables:

The scope is the current file, from the definition/declaration position to the end of the file.

The life cycle is from the program running to the program exit, that is, throughout the entire running time.

The default initialization value is 0 when there is no explicit initialization.

3 Dynamic local variables:

The scope is the current function, from the definition position to the end position of {} where it is located.

The life cycle is from function call to function exit.

The default initialization value is a random value when there is no explicit initialization.

4 Static local variables:

The scope is the current function, from the definition position to the end position of {} where it is located.

The life cycle is from the program running to the program exit, that is, throughout the entire running time. When the next function is called, the static local variables will not be initialized again, but will use the value when the last function exits.

The default initialization value is 0 when there is no explicit initialization.

Recommended course: C language tutorial

The above is the detailed content of ​Static variables and dynamic variables. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template