当前位置:网站首页>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; //错误!只读变量的值不能改变。
注意这里是声明不是定义
边栏推荐
- SQL Server生成自增序号
- 414. The third largest digital buckle
- MariaDB database management system learning (I) installation diagram
- LeetCode刷题(十一)——顺序刷题51至55
- 0 basic learning C language - digital tube
- HDU 4912 paths on the tree (lca+)
- 硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
- Oracle-控制文件及日志文件的管理
- Attack and defense world ditf Misc
- AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
猜你喜欢
HDR image reconstruction from a single exposure using deep CNNs阅读札记
基於 QEMUv8 搭建 OP-TEE 開發環境
BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
Build op-tee development environment based on qemuv8
Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"
CCNA-思科网络 EIGRP协议
Xiaoman network model & http1-http2 & browser cache
C # réalise la liaison des données du rapport Crystal et l'impression du Code à barres 4
图像的spatial domain 和 frequency domain 图像压缩
【10点公开课】:视频质量评价基础与实践
随机推荐
Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"
Unity3d Learning Notes 6 - GPU instantiation (1)
BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
UNI-Admin基础框架怎么关闭创建超级管理员入口?
RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv
GPS from getting started to giving up (XX), antenna offset
Solve project cross domain problems
Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
Build op-tee development environment based on qemuv8
AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
[sciter bug] multi line hiding
GPS from getting started to giving up (19), precise ephemeris (SP3 format)
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
Oracle control file and log file management
Learn the principle of database kernel from Oracle log parsing
Crawler obtains real estate data
Management background --4, delete classification
GPS from getting started to giving up (16), satellite clock error and satellite ephemeris error