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

How to use static in C language

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to use static in C language, the article is very detailed, has a certain reference value, interested friends must read it!

1.static can modify local variables

First of all, let me look at this code.

# includevoid test () {int a = 1; a = a + 1; printf ("% d", a);} int main () {int I = 0; while (I)

< 10) { test(); i++; } return 0;} 你认为输出的结果是什么呢? 那为什么结果是2222222222呢

Because the local variable an enters this range and creates this range to destroy, the result is 2.

What will be the result when we add static

Then why is the result different with static?

Let's study the principle of using static.

Our memory is divided into three categories.

Our local variables are placed in the stack area

And static put an in the static zone, and the variables in the static zone are out of his range and will not be destroyed.

It may be more intuitive if we compare it.

So when static modifies a local variable, it actually changes where the local variable has to be stored.

The static local variables are placed in the static zone, and the variables placed in the static zone will not be destroyed if they are out of scope, which is equivalent to a longer life cycle.

The scope of the supplementary variable is unchanged, but the life cycle is prolonged.

2. Static can modify global variables

Let's create another source file

The scope of the global variable is the entire project

If we want to reference the function of another source file, we have to say the keyword extern here.

As shown in the picture

Static can modify global variables. What would happen if we added static before the global variable?

The error will be reported at this time.

A global variable has an external link attribute (that is, it can be referenced by other source files), but after being modified by static, the external link attribute becomes an internal link attribute that can only be used within its own source file, not within other files.

So the error will be reported when debugging again.

The essence of the global variable modified by static is that the external link property changes rather than its scope becomes smaller.

The meaning of static decorating a global variable is that if you define a global variable, others can use it with extern.

You can use static if you don't want others to see it.

3.static can modify a function

We use another source file to create a function as shown in the figure

Debug results after declaration with the keyword extern

What will be the result if we add static before the function here?

In fact, a function itself has an external link property, after the external link property is modified by static, the external link property becomes an internal link property, which can only be used within the source 2.c when other source files cannot be used.

We can find that static modifies global variables in the same way as decorating functions

The above is all the content of the article "how to use static in C language". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow 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