当前位置:网站首页>存储类别
存储类别
2022-07-29 05:08:00 【Ryan菲特】
存储类别
1.作用域
作用域描述程序中可访问标识符的区域。其主要有:块作用区域、函数作用域、函数原型作用域、文件作用域。
2.链接
C变量有3种链接属性:外部链接、内部链接、无链接。其中块作用域、函数作用域、函数原型作用域的变量都是无链接变量。
3.存储期
作用域和链接描述了标识符的可见性。存储期描述了通过这些标识符访问的对象的生存期。C有4种存储期:静态存储期、线程存储期、自动存储期、动态分配存储期。
4.5种存储类别
| 存储类别 | 存储期 | 作用域 | 链接 | 声明方式 |
|---|---|---|---|---|
| 自动 | 自动 | 块 | 无 | 块内 |
| 寄存器 | 自动 | 块 | 无 | 块内,使用关键字register |
| 静态外部链接 | 静态 | 文件 | 外部 | 所有函数外 |
| 静态内部链接 | 静态 | 文件 | 内部 | 所有函数外,使用关键字static |
| 静态无链接 | 静态 | 块 | 无 | 块内,使用关键字static |
//块中的变量
#include <stdio.h>
int main(void)
{
int x = 30;//原始的x
printf("x in outer block :%d at %p\n", x, &x);
{
int x = 77;
printf("x in inner block:%d at %p\n", x, &x);
}
printf("x in outer block :%d at %p\n", x, &x);
while (x++ < 33)//原始x
{
int x = 100;//新的x,隐藏了原始的x
x++;
printf("x in while loop:%d at %p\n", x, &x);
}
printf("x in outer block :%d at %p\n", x, &x);
return 0;
}
程序输出:
x in outer block :30 at 000000000061fe08
x in inner block:77 at 000000000061fe0c
x in outer block :30 at 000000000061fe08
x in while loop:101 at 000000000061fe0c
x in while loop:101 at 000000000061fe0c
x in while loop:101 at 000000000061fe0c
x in outer block :34 at 000000000061fe08
以上程序表现了在不同块中的变量的作用以 “{}” 作为分界。
5.存储类别说明符
| 关键字 | 说明 |
|---|---|
| typedef | 与任何内存都无关,在绝大多数情况下,不能在声明中使用多个存储类别说明符,所以不能使用多个存储类别说明符作为typedef的一部分。 |
| auto | 表明变量是自动存储期,只能用于块作用域的变量声明中。 |
| register | 只用于块作用域的变量,把变量归为寄存器存储类别,请求最快速度访问该变量,同时也保护了变量的地址不被获取。 |
| static | 说明创建的对象具有静态存储期,载入程序时创建对象,当程序结束时对象消失。块作用域的静态变量无链接;文件作用域的静态变量具有内部链接。 |
| extern | 表明声明的变量定义在别处。若包含extern的声明具有文件作用域,则引用的变量必须具有外部链接。若包含extern的声明具有块作用域,则引用的变量可能具有外部链接或内部链接,取决于变量的定义式声明。 |
边栏推荐
- ARFoundation从零开始8-Geospatial API(地理空间)开发
- Arfoundation starts from zero 9-ar anchor
- 【赛事预告】云上开发,高效智能——第二届阿里云ECS CloudBuild开发者大赛即将启动
- OCCT学习003-----MFC单文档工程
- ARFoundation入门教程7-url动态加载图像跟踪库
- 数千个数据库、遍布全国的物理机,京东物流全量上云实录 | 卓越技术团队访谈录
- C语言文件操作
- C语言连连看秒杀辅助
- MySQL sorts the queried result set according to the specified sequence
- C how to realize simple factory mode
猜你喜欢

【赛事预告】云上开发,高效智能——第二届阿里云ECS CloudBuild开发者大赛即将启动

6.3 references

京东云金秋上云特惠进行中!扫码参与活动

510000 prize pool invites you to fight! The second Alibaba cloud ECS cloudbuild developer competition is coming

C语言连连看秒杀辅助

QML type: mousearea

ARFoundation从零开始8-Geospatial API(地理空间)开发

OCCT学习003-----MFC单文档工程

Arfoundation starts from scratch 8-geospatial API (geospatial) development

串口通讯部分详解
随机推荐
MySQL sorts the queried result set according to the specified sequence
OCCT学习001-----简介
APP常用跨端技术栈深入分析
In depth analysis of common cross end technology stacks of app
Live broadcast preview | how to improve enterprise immunity through "intelligent edge security"?
Scikit learn -- steps and understanding of machine learning application development
osgSimplegl3结合RenderDoc工具
【[第一次写博客]Uda课程中的P控制器实现说明】
京东云金秋上云特惠进行中!扫码参与活动
QML custom tabbar
MySQL的详细安装使用教程(保姆式安装图文讲解)
Qml类型:State 状态
How to get command parameters in Visual Basic.Net
Arfoundation starts from scratch 3- create an arfoundation project
手写学生管理系统
Xiaolu Inn - Trailer
Alibaba cloud architect Liang Xu: MES on cloud box helps customers quickly build digital factories
C语言函数实现输出I love you
ARFoundation入门教程10-平面检测和放置
Yangyonglin, vice president of Rushi Technology: when traditional industries encounter "digital space"