当前位置:网站首页>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; //错误!只读变量的值不能改变。
注意这里是声明不是定义
边栏推荐
- Four data streams of grpc
- (十八)LCD1602实验
- Support multiple API versions in flask
- i. Mx6ull build boa server details and some of the problems encountered
- 12、 Start process
- Shortcut keys in the terminal
- Inno Setup 打包及签名指南
- 【10点公开课】:视频质量评价基础与实践
- PVL EDI project case
- Build op-tee development environment based on qemuv8
猜你喜欢
![[MySQL] online DDL details](/img/7e/97098d7ed5802c446bbadaf7035981.png)
[MySQL] online DDL details

labelimg的安装与使用

Unity3d Learning Notes 6 - GPU instantiation (1)

Leetcode question brushing (XI) -- sequential questions brushing 51 to 55

重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障

二分图判定

C # realizes crystal report binding data and printing 4-bar code

C#实现水晶报表绑定数据并实现打印4-条形码

Common sense: what is "preservation" in insurance?

About the professional ethics of programmers, let's talk about it from the way of craftsmanship and neatness
随机推荐
【10点公开课】:视频质量评价基础与实践
2021 geometry deep learning master Michael Bronstein long article analysis
Solve project cross domain problems
Crawler obtains real estate data
Oracle Performance Analysis 3: introduction to tkprof
网络基础入门理解
OpenCV VideoCapture. Get() parameter details
[sciter bug] multi line hiding
CCNA-思科网络 EIGRP协议
Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing
基于 QEMUv8 搭建 OP-TEE 开发环境
Installation and use of labelimg
解决项目跨域问题
[leetcode daily clock in] 1020 Number of enclaves
zabbix 代理服务器 与 zabbix-snmp 监控
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
QT | UDP broadcast communication, simple use case
0 basic learning C language - digital tube
Common sense: what is "preservation" in insurance?
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件