ANSI-C 中的静态是什么意思 [英] What does static mean in ANSI-C

查看:24
本文介绍了ANSI-C 中的静态是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
什么是静态"?在 C 程序中是什么意思?

static 关键字在 C 中是什么意思?

What does the static keyword mean in C ?

我使用的是 ANSI-C.我在几个代码示例中看到,它们在变量和函数前使用 static 关键字.与变量一起使用的目的是什么?和函数一起使用的目的是什么?

I'm using ANSI-C. I've seen in several code examples, they use the static keyword in front of variables and in front of functions. What is the purpose in case of using with a variable? And what is the purpose in case of using with a function?

推荐答案

简单回答一下,static关键字在定义变量时有两种用法:

Just as a brief answer, there are two usages for the static keyword when defining variables:

1- 使用 static 关键字在文件范围内定义的变量, 定义的外部函数将仅在该文件内可见.任何从其他文件访问它们的尝试都将导致链接时无法解析符号.

1- Variables defined in the file scope with static keyword, i.e. defined outside functions will be visible only within that file. Any attempt to access them from other files will result in unresolved symbol at link time.

2- 在函数内的块内定义为 static 的变量将在同一代码块的不同调用中持续存在或存活".如果它们被定义为初始化,那么它们只被初始化一次.static 变量通常保证默认初始化为 0.

2- Variables defined as static inside a block within a function will persist or "survive" across different invocations of the same code block. If they are defined initialized, then they are initialized only once. static variables are usually guaranteed to be initialized to 0 by default.

这篇关于ANSI-C 中的静态是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆