当前位置:网站首页>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; //错误!只读变量的值不能改变。
注意这里是声明不是定义
边栏推荐
- MySQL数据库基本操作-DML
- SQL Server生成自增序号
- Anaconda installs third-party packages
- A Mexican airliner bound for the United States was struck by lightning after taking off and then returned safely
- Oracle Performance Analysis 3: introduction to tkprof
- Kohana database
- Chapter 4: talk about class loader again
- PVL EDI 项目案例
- Oracle-控制文件及日志文件的管理
- C # réalise la liaison des données du rapport Crystal et l'impression du Code à barres 4
猜你喜欢
Installation and use of labelimg
Learn the principle of database kernel from Oracle log parsing
RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv
Search element topic (DFS)
ZABBIX proxy server and ZABBIX SNMP monitoring
Management background --4, delete classification
[sciter]: encapsulate the notification bar component based on sciter
第4章:再谈类的加载器
LeetCode 练习——剑指 Offer 26. 树的子结构
重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障
随机推荐
BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
Powerful domestic API management tool
中国VOCs催化剂行业研究与投资战略报告(2022版)
Embedded common computing artifact excel, welcome to recommend skills to keep the document constantly updated and provide convenience for others
2500个常用中文字符 + 130常用中英文字符
Barcodex (ActiveX print control) v5.3.0.80 free version
中国固态氧化物燃料电池技术进展与发展前景报告(2022版)
做接口测试都测什么?有哪些通用测试点?
GPS from getting started to giving up (XI), differential GPS
图像的spatial domain 和 frequency domain 图像压缩
Aardio - 封装库时批量处理属性与回调函数的方法
extern关键字
Learn the principle of database kernel from Oracle log parsing
Anaconda installs third-party packages
Attack and defense world miscall
Installation and use of labelimg
2021 geometry deep learning master Michael Bronstein long article analysis
将MySQL的表数据纯净方式导出
[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation
C # réalise la liaison des données du rapport Crystal et l'impression du Code à barres 4