#define vs. const vs enum
In one word, using const is better than define. enum is the best.
There are lots of discussions. I put a great paper below.
https://betterembsw.blogspot.com/2017/05/define-vs-const.html
The compiler might generate storage for a constant object, even though the program doesn't really need that storage. It might even generate additional code to initialize the constant at run time.
Another way is using enum, which will not generate the additional code.
example.
uint8_t GetAddressValues(void)
{
/* The counter of ADC channel
* There are four channels for reading 1, 2, 4, 8
* who corresponding to bit 0, bit 1, bit 2, bit 3
*/
//#define ADC_CHANNELS_COUNT (4)
//const uint8_t ADC_CHANNELS_COUNT = 4; /* select the ADC with VR+ = VCC and VR- = VSS
* 16 × ADC10CLKs, ADC10 on, Interrupt disabled
* HIGH_LEVEL_VALUE is the value of high level gate
* it is high level when it is greater than HIGH_LEVEL_VALUE,
* otherwise, it is low.
*
* HIGH_LEVEL_VALUE is based on the Vin of high level (0.424V)
* HIGH_LEVEL_VALUE = 1023 * 0.424/3.3 = 131.44
*/
//#define HIGH_LEVEL_VALUE (132)
//const uint8_t HIGH_LEVEL_VALUE = 132; enum
{
ADC_CHANNELS_COUNT = , //ADC channels counter
HIGH_LEVEL_VALUE = , //high level threshold
};
}
#define vs. const vs enum的更多相关文章
- EC笔记,第一部分:2.尽量以const,enum,inline代替#define
02.尽量以const,enum,inline代替#define 原因:编译前的预处理会替换宏,所以调试的时候找不到错误 1.const 尽量用const替代常量宏定义 两种特殊情况: (1).常量指 ...
- Effective C++ 之 Item 2:尽量以 const, enum, inline 替换 #define
Effective C++ Chapter 1. 让自己习惯C++(Accustoming Yourself to C++) Item 2. 尽量以 const, enum, inline 替换 #d ...
- 条款2:尽量以const、enum、inline替换#define
1> 以const替换#define • 比如用const double Ratio = 1.653替换#define RATIO 1.653 因为宏定义在预处理阶段就会被替换成其所指代的内容, ...
- C++学习之路(七):以const,enum,inline替换#define
这篇博文主要是编程中的一些问题和技巧.如题目所示,这些关键字的作用不再进行描述.直接描述功能和实例代码. 首先,在头文件中对类进行定义,是不会为类分配内存空间的,在这一点上类定义可以和普通变量类型的声 ...
- 【Effective C++ 读书笔记】条款02: 尽量以 const, enum, inline 替换 #define
条款02: 尽量以 const, enum, inline 替换 #define 这个条款或许可以改为“宁可以编译器替换预处理器”. 编译过程: .c文件--预处理-->.i文件--编译--&g ...
- iOS学习——#define、const、typedef的区别
在iOS开发中经常遇到一些字段和类型的定义,例如配置生产和测试不同环境的参数等,这时候经常用到#define.const以及typedef.那么它们之间有什么区别呢?我们接下来一个一个具体了解下. 一 ...
- c++中typedef、define、const、inline之间的区别
1.typedef和#define的区别 typedef int* pInt; , b = ; const pInt p1 = &a; //p1是常量指针 pInt const p2 = &a ...
- #define、const、typedef的区别
#define.const.typedef的区别 #define 并不是定义变量, 只是用来做文本替换 例如: #define PI 3.1415926 float angel; angel=30*P ...
- IOS 基础-define、const、extern、全局变量
这里介绍一下define.const.extern的用法.优劣以及要注意的地方. 1.define 宏define是定义一个变量,没有类型信息.define定义的常量在内存中有若干个拷贝. defin ...
随机推荐
- [leetcode53]最长子数组 Maximum Subarray Kadane's算法
[题目] Given an integer array nums, find the contiguous subarray (containing at least one number) whic ...
- Linux3.10.0块IO子系统流程(3)-- SCSI策略例程
很长时间以来,Linux块设备使用了一种称为“蓄流/泄流”(plugging/unplugging)的技术来改进吞吐率.简单而言,这种工作方式类似浴盆排水系统的塞子.当IO被提交时,它被储存在一个队列 ...
- SQL-17 获取当前(to_date='9999-01-01')薪水第二多的员工的emp_no以及其对应的薪水salary
题目描述 获取当前(to_date='9999-01-01')薪水第二多的员工的emp_no以及其对应的薪水salaryCREATE TABLE `salaries` (`emp_no` int(11 ...
- mybatis动态sql #和$的区别
$和#都支持动态sql:就是你传什么它就是什么 区别: 1.#可以防止sql注入在sql执行时显示 '?' 比$安全 SELECT * FROM table WHERE id = ? 2.在使用#传入 ...
- FPGA构造spi时序——AD7176为例(转)
reference:https://blog.csdn.net/fzhykx/article/details/79490330 项目中用到了一种常见的低速接口(spi),于是整理了一下关于spi相关的 ...
- 安装连接mysql8时候遇到的问题以及解决(转)
官网下载mysql8的安装包: https://dev.mysql.com/downloads/ 下一步安装即可. mysql8增加了传说中的安全性校验 遇到的几个问题: 1.natcat连接不上.参 ...
- Linux文件系统命令 rm
命令名:rm 功能:删除某一个文件或者目录 eg: renjg@renjg-HP-Compaq-Pro--MT:~/WorkSpace$ ls BM3 gf k8s minicom_download ...
- oracle 实例名,数据库名概念
拷贝于https://www.cnblogs.com/ahudyan-forever/p/6016784.html 在实际的开发应用中,关于Oracle数据库,经常听见有人说建立一个数据库,建立一个I ...
- <Spark><Programming><Key/Value Pairs><RDD>
Working with key/value Pairs Motivation Pair RDDs are a useful building block in many programs, as t ...
- spark:ML和MLlib的区别
ML和MLlib的区别如下: ML是升级版的MLlib,最新的Spark版本优先支持ML. ML支持DataFrame数据结构和Pipelines,而MLlib仅支持RDD数据结构. ML明确区分了分 ...