
共享酷好,传播怡悦,增长见闻,留住好意思好!
亲爱的您,这里是LearningYard新学苑。
今天小编为公共带来
“C话语存储类”。
接待您的观测!
Share interest, spread happiness, increase knowledge, and leave beautiful.
Dear, this is the LearingYard New Academy!
Today, the editor brings the "Storage Classes in C Language".
Welcome to visit!
想维导图
Mind mapping
存储类是C话语顶用于界说变量/函数的存储位置、生命周期和作用域的中枢属性,决定了变量在内存中的存在款式和观测模范。不同存储类的变量在内存分派、存活技能、可见规模上存在显赫各异,合理遴荐存储类是优化内存使用、晋升门径健壮性的要道。
Storage classes are core attributes in the C language used to define the storage location, lifetime, and scope of variables/functions, determining the existence form and access rules of variables in memory. Variables of different storage classes have significant differences in memory allocation, survival time, and visibility range. Reasonable selection of storage classes is the key to optimizing memory usage and improving program robustness.
存储类的分类与界说
Classification and Definition of Storage Classes
C话语提供4种中枢存储类,通过要道字修饰变量/函数完毕,每种存储类适配不同的使用场景:
The C language provides 4 core storage classes, implemented by modifying variables/functions with keywords, and each storage class is adapted to different usage scenarios:
{jz:field.toptypename/}1. 自动存储类(auto):默许的存储类,仅适用于局部变量(函数/代码块里面)。要道字`auto`可不祥(如`auto int a;`等价于`int a;`),变量在门径推行到界说刑事职守配栈内存,离开作用域时立即开释,生命周期与作用域十足一致,无默许运行值(值为就地垃圾值)。
1. Automatic Storage Class (auto): The default storage class, applicable only to local variables (inside functions/code blocks). The keyword `auto` can be omitted (e.g., `auto int a;` is equivalent to `int a;`). Variables allocate stack memory when the program executes to the definition, and release it immediately when leaving the scope. The lifetime is completely consistent with the scope, and there is no default initial value (the value is a random garbage value).
2. 寄存器存储类(register):用于修饰局部变量,申请编译器将变量存储在CPU寄存器中(而非内存),以晋升观测速率。语法为`register int count;`,需夺目:寄存器数目有限,编译器可能忽略该申请;无法获得寄存器变量的地址(`&`运算符弗成用);仅适用于高频观测的短小变量(如轮回计数器)。
2. Register Storage Class (register): Used to modify local variables, requesting the compiler to store the variable in a CPU register (instead of memory) to improve access speed. The syntax is `register int count;`. Note: The number of registers is limited, and the compiler may ignore this request; the address of a register variable cannot be obtained (the `&` operator is unavailable); it is only applicable to short variables with high-frequency access (such as loop counters).
3. 静态存储类(static):可修饰局部变量和全局变量,中枢作用是放手作用域或蔓延生命周期。修饰局部变量时,变量存储在全局数据区,函数调用终局后值不丢失(生命周期与门径一致),但作用域仍限于原函数;修饰全局变量/函数时,作用域被放手在现时源文献,其他文献无法通过`extern`观测,幸免定名突破。
3. Static Storage Class (static): Can modify local variables and global variables,米兰体育官网 with the core function of limiting the scope or extending the lifetime. When modifying local variables, the variable is stored in the global data area, and the value is not lost after the function call ends (the lifetime is consistent with the program), but the scope is still limited to the original function; when modifying global variables/functions, the scope is limited to the current source file, and other files cannot access them through `extern`, avoiding naming conflicts.
4. 外部存储类(extern):仅用于声明已在其他位置界说的全局变量/函数,完毕跨文献观测,自己不分派内存。语法为`extern double pi;`,需夺目:`extern`仅作念声明,变量的界说必须在某个源文献中完成;若全局变量在界说时运行化,`extern`声明时无需重叠赋值。
4. Extern Storage Class (extern): Only used to declare global variables/functions defined elsewhere to achieve cross-file access, and does not allocate memory itself. The syntax is `extern double pi;`. Note: `extern` only makes a declaration, and the definition of the variable must be completed in a source file; if a global variable is initialized at definition, there is no need to reassign it in the `extern` declaration.
存储类的中枢特色对比
Comparison of Core Characteristics of Storage Classes
1. 自动存储类(auto):仅适用于局部变量,存储在栈内存中,生命周期仅限于其场所的函数或代码块作用域内,作用域也仅为函数/代码块里面,无默许运行值,值为就地垃圾值。
1. Automatic Storage Class (auto): Applicable only to local variables, stored in stack memory, with a lifetime limited to the scope of the function or code block where it is located, and the scope is also only inside the function/code block, with no default initial value (the value is a random garbage value).
2. 寄存器存储类(register):仅适用于局部变量,米兰优先存储在CPU寄存器中(编译器若忽略申请则存储在栈内存),生命周期和作用域均限于函数/代码块里面,无默许运行值,值为就地垃圾值。
2. Register Storage Class (register): Applicable only to local variables, preferentially stored in CPU registers (if the compiler ignores the request, it is stored in stack memory), with both lifetime and scope limited to the inside of the function/code block, and no default initial value (the value is a random garbage value).
3. 静态存储类(static):可适用于局部变量和全局变量,存储在全局数据区,生命周期疏导通盘门径运行经由;其中静态局部变量的作用域限于界说它的函数里面,静态全局变量/函数的作用域仅为现时源文献,默许运行值为0。
3. Static Storage Class (static): Applicable to local variables and global variables, stored in the global data area, with a lifetime throughout the entire program runtime; the scope of static local variables is limited to the inside of the function where they are defined, the scope of static global variables/functions is only the current source file, and the default initial value is 0.
4. 外部存储类(extern):适用于全局变量和函数,存储在全局数据区,生命周期疏导通盘门径运行经由;经过extern声光芒,作用域可遮蔽通盘源文献,默许运行值为0。
4. Extern Storage Class (extern): Applicable to global variables and functions, stored in the global data area, with a lifetime throughout the entire program runtime; after being declared with extern, the scope can cover all source files, and the default initial value is 0.
存储类使用的中枢夺目事项
Key Notes for Using Storage Classes
1. 幸免蹧跶`static`:静态局部变量会长久占用内存,相似使用会加多内存支拨;静态全局变量虽能幸免突破,但会镌汰代码模块化进程,需严慎使用。
1. Avoid Abuse of `static`: Static local variables occupy memory for a long time, and frequent use will increase memory overhead; although static global variables can avoid conflicts, they will reduce the modularity of the code and need to be used carefully.
2. `register`的合理使用:仅对高频观测的变量(如轮回变量`i`)使用`register`,编译器对经常变量的`register`修饰泛泛会忽略,无需盲目添加。
2. Rational Use of `register`: Use `register` only for variables with high-frequency access (such as loop variable `i`). The compiler usually ignores the `register` modification of ordinary variables, so there is no need to add it blindly.
3. `extern`的跨文献标准:跨文献使用全局变量时,提议在头文献顶用`extern`声明,在一个源文献中界说并运行化,幸免多个源文献重叠界说导致接续特地。
3. Cross-File Specification of `extern`: When using global variables across files, it is recommended to declare them with `extern` in the header file, and define and initialize them in one source file to avoid linking errors caused by repeated definitions in multiple source files.
4. 存储类与内存恶果:栈内存(auto/register)分派开释恶果高,但空间有限;全局数据区(static/extern)空间大,但生命周期长,需凭证变量使用频率和存活技能遴荐存储类。
4. Storage Classes and Memory Efficiency: Stack memory (auto/register) has high allocation and release efficiency but limited space; global data area (static/extern) has large space but long lifetime. It is necessary to select the storage class according to the frequency of use and survival time of the variable.
讲究
Summary
C话语存储类包含auto、register、static、extern四类,中枢各异体当今存储位置、生命周期和作用域上:auto/register为局部变量专属,存储在栈/寄存器,生命周期短;static/extern存储在全局数据区,生命周期疏导门径全程,其中static放手作用域,extern推广跨文献作用域。合理遴荐存储类需均衡内存恶果与观测需求,幸免蹧跶static和盲目使用register。
Storage classes in C language include four types: auto, register, static, and extern. The core differences are reflected in storage location, lifetime and scope: auto/register are exclusive to local variables, stored in stack/registers with short lifetimes; static/extern are stored in the global data area with lifetimes throughout the program runtime, among which static limits the scope and extern extends cross-file scope. Reasonable selection of storage classes needs to balance memory efficiency and access requirements, avoiding abuse of static and blind use of register.
今天的共享就到这里了,
要是您对著作有私有的主义,
接待给咱们留言。
让咱们相约未来,
祝您今天过得欣忭怡悦!
That's all for today's sharing.
If you have a unique idea about the article,
please leave us a message,
and let us meet tomorrow.
I wish you a nice day!
翻译:文心一言
参考贵府:百度百科
本文由LearningYard新学苑整理并发出,如有侵权请后台留言交流。
案牍&排版|qiu
审核|song

备案号: