当前位置:网站首页>const关键字
const关键字
2022-07-06 14:40:00 【是北豼不太皮吖】
const 修饰的只读变量
很多人都认为被 const 修饰的值是常量。这是不精确的,精确的说应该是只读的变量,其值在编译时不能被使用,因为编译器在编译时不知道其存储的内容。
const 推出的初始目的,正是为了取代预编译指令,消除它的缺点,同时继承它的优点。我们看看它与 define 宏的区别。(很多人误以为 define 是关键字,define是编译器的预编译指令,是编译器实现的,不是C语言的内容。)
定义 const 只读变量,具有不可变性。
const int Max=100;
int Array[Max];
在 Visual C++6.0 里分别创建.c 文件和.cpp 文件测试一下。你会发现在.c 文件中,编译器会提示出错,而在.cpp 文件中则顺利运行。为什么呢?我们知道定义一个数组必须指定其元素的个数。这也从侧面证实在 C 语言中,const 修饰的 Max 仍然是变量,只不过是只读属性罢了;
const 修饰的只读变量必须在定义的同时初始化,为什么?
答:因为定义的变量被修饰成只读变量了,在定义后就不能被修改,所以定义的时候一定要初始化。
case 语句后面是否可以是 const 修饰的只读变量呢?
答:不行,const修饰后为只读变量但本质还是变量,而case语句后面的需要整型、字符型的常量、常量表达式
节省空间,避免不必要的内存分配,同时提高效率
编译器通常不为普通 const 只读变量分配存储空间,而是将它们保存在符号表中,这使得它成为一个编译期间的值,没有了存储与读内存的操作,使得它的效率也很高。
#define M 3 //宏常量
const int N=5; //此时并未将 N 放入内存中
......
int i=N; //此时为 N 分配内存,以后不再分配!
int I=M; //预编译期间进行宏替换,分配内存
int j=N; //没有内存分配
int J=M; //再进行宏替换,又一次分配内存!
const 定义的只读变量从汇编的角度来看,只是给出了对应的内存地址,而不是象#define一样给出的是立即数,所以,const 定义的只读变量在程序运行过程中只有一份拷贝(因为它是全局的只读变量,存放在静态区),而#define 定义的宏常量在内存中有若干个拷贝。#define 宏是在预编译阶段进行替换,而 const 修饰的只读变量是在编译的时候确定其值。#define 宏没有类型,而 const 修饰的只读变量具有特定的类型。
修饰一般变量
一般常量是指简单类型的只读变量。这种只读变量在定义时,修饰符 const 可以用在类型说明符前,也可以用在类型说明符后。例如:
int const i=2;
或
const int i=2;
修饰数组
定义或说明一个只读数组可采用如下格式:
int const a[5]={
1, 2, 3, 4, 5};
或
const int a[5]={
1, 2, 3, 4, 5};
修饰指针
const int *p; // p 可变,p 指向的对象不可变
int const *p; // p 可变,p 指向的对象不可变
int *const p; // p 不可变,p 指向的对象可变
const int *const p; //指针 p 和 p 指向的对象都不可变
先忽略类型名(编译器解析的时候也是忽略类型名),我们看 const 离哪个近。“近水楼台先得月”,离谁近就修饰谁。
const int *p; //const 修饰*p,p 是指针,*p 是指针指向的对象,不可变
int const *p; //const 修饰*p,p 是指针,*p 是指针指向的对象,不可变
int *const p; //const 修饰 p,p 不可变,p 指向的对象可变
const int *const p; //前一个 const 修饰*p,后一个 const 修饰 p,指针 p 和 p 指向的对象都不可变
修饰函数的参数
const 修饰符也可以修饰函数的参数,当不希望这个参数值被函数体内意外改变时使用。例如:
void Fun(const int i);
告诉编译器 i 在函数体中的不能改变,从而防止了使用者的一些无意的或错误的修改。
修饰函数的返回值
const 修饰符也可以修饰函数的返回值,返回值不可被改变。例如:
const int Fun (void);
在另一连接文件中引用 const 只读变量:
extern const int i; //正确的声明
extern const int j=10; //错误!只读变量的值不能改变。
注意这里是声明不是定义
边栏推荐
- AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
- C # réalise la liaison des données du rapport Crystal et l'impression du Code à barres 4
- UNI-Admin基础框架怎么关闭创建超级管理员入口?
- GPS from getting started to giving up (XI), differential GPS
- 将MySQL的表数据纯净方式导出
- 墨西哥一架飞往美国的客机起飞后遭雷击 随后安全返航
- Four data streams of grpc
- Notes de développement du matériel (10): flux de base du développement du matériel, fabrication d'un module USB à RS232 (9): création de la Bibliothèque d'emballage ch340g / max232 SOP - 16 et Associa
- PVL EDI 项目案例
- Unity3d Learning Notes 6 - GPU instantiation (1)
猜你喜欢

Embedded common computing artifact excel, welcome to recommend skills to keep the document constantly updated and provide convenience for others

Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题

ZABBIX proxy server and ZABBIX SNMP monitoring

HDR image reconstruction from a single exposure using deep CNNs阅读札记

Attack and defense world ditf Misc

【数字IC手撕代码】Verilog无毛刺时钟切换电路|题目|原理|设计|仿真

labelimg的安装与使用

RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv

HDR image reconstruction from a single exposure using deep CNN reading notes

Adjustable DC power supply based on LM317
随机推荐
China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)
GD32F4XX串口接收中断和闲时中断配置
Management background --4, delete classification
OpenCV VideoCapture. Get() parameter details
Spatial domain and frequency domain image compression of images
BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
Attack and defense world ditf Misc
ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
[线性代数] 1.3 n阶行列式
How does the uni admin basic framework close the creation of super administrator entries?
Oracle Performance Analysis 3: introduction to tkprof
i. Mx6ull build boa server details and some of the problems encountered
12、 Start process
Force deduction question 500, keyboard line, JS implementation
每日一题:力扣:225:用队列实现栈
Mongodb (III) - CRUD
小常识:保险中的“保全”是什么?
中国1,4-环己烷二甲醇(CHDM)行业调研与投资决策报告(2022版)
在IPv6中 链路本地地址的优势
Leetcode question brushing (XI) -- sequential questions brushing 51 to 55