Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Global variables and Local variables in C language

2025-04-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you the example analysis of global variables and local variables in the C language, I believe most people still do not know how, so share this article for your reference, I hope you have a lot of harvest after reading this article, let us go to understand it together!

1: Local variables and global variables 1.1: Local variables

Local variable: variable defined inside the function, auto may be added or not

Scope: from definition to the end of this function

Life cycle: from definition to end of function

1.2: Global variables

Global variables: variables defined outside the function, so there is a data area

Scope: All files in the project.

Note: Use extern int a in other files, extern is a keyword, dedicated to declare external symbols.

Life cycle: program creation to program destruction.

1.3: Code interpretation

Note: Local variables are preferred when they have the same name as global variables.

1.4: modification of const-modified variables

global variables

Failed to modify directly

The reason for indirect modification failure is placed in the constant area and protected.

local variables

Failed to modify directly

The reason for indirect modification success is placed on the stack. Local variables exist in the stack area.

See my C Pointer Basics for details

2: Static local variables and static global variables 2.1: static keywords

static :

Decorate local variables: actually change the storage type of local variables,(from stack area to static area). This allows static local variables to be removed from scope and not destroyed. There is actually a "hedge" effect on local variables.

Modify global variable: a global variable can be used in other files of the whole project, because the global variable has an external link attribute. When static is modified, the external link attribute becomes an internal link attribute, so that this global variable can only be used in its own source file, and other files cannot be used.

2.2 static local variable

Static local variable: A variable defined inside a function, modified with static, that holds a value in the static region (hedging).

Scope: In the current function.

Life cycle: program creation to program destruction

Static local variables are initialized only once and can be assigned multiple times

Storage in data area (static area)

2.3: static global variable

Static global variables: variables defined outside the function, using static modification, can be used in this file, can not be used in other files of the same project.

Scope: In the current file.

Life cycle: program creation to program destruction.

Note: Global variables, static local variables, static global variables If the system is not initialized, the default initialization value is 0. Local variables need to be initialized.

2.4: Summary

3: Global and Static Functions 3.1: Global Functions

Global function: A function without any decoration.

Scope: All files in the project (other files need to be declared when used).

Life cycle: program creation to program destruction.

3.2: static function

Static functions: Functions that use static modifiers.

Scope: In the current file.

Life cycle: program creation to program completion.

Note: Static functions can have the same name as global functions and obey the principle of proximity.

3.3: summary table

The above is "C language global variables and local variables in the sample analysis" of all the contents of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report