#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 ...
随机推荐
- 深入理解java虚拟机---JDK8-废弃永久代(PermGen)迎来元空间(Metaspace)(十二)
引用:https://www.cnblogs.com/yulei126/p/6777323.html JDK8-废弃永久代(PermGen)迎来元空间(Metaspace) 1.背景 2.为什么废 ...
- 第六节 静态的(static)和单例模式
main函数 主函数是一个特殊的函数,作为程序的入口,可以被jvm(虚拟器)调用 主函数的定义 public 表示该函数的访问权限是最大的. static 代表主函数随着类的加载就已经存在了. voi ...
- 【重大更新】DevExpress WinForms v18.2新版亮点(七)
买 DevExpress Universal Subscription 免费赠 万元汉化资源包1套! 限量15套!先到先得,送完即止!立即抢购>> 行业领先的.NET界面控件2018年第 ...
- 关于jvm钩子 Runtime.getRuntime().addShutdownHook
转自: http://www.cnblogs.com/nexiyi/p/java_add_ShutdownHook.html 在线上Java程序中经常遇到进程程挂掉,一些状态没有正确的保存下来,这时候 ...
- h5页面嵌入android app时遇到的问题
1.h5页面 通过 .css("transform") 或 .style.transform 获取 transform属性,并通过 split 方法解析 页面translateY ...
- restful接口设计规范总结
这篇 文章主要是借鉴他人,但是自己很想总结出一套规范,以供向我这样的新手使用,用来规范代码,如果有什么好的提议,请不吝赐教,本篇文章长期更新! 一.重要概念: REST,即Representation ...
- 如何给cbv的程序添加装饰器
引入method_decorator模块 1,直接在类上加装饰器 @method_decorator(test,name=‘dispatch’) class Loginview(view) 2,直接在 ...
- JAVA 线程Join
join方法: 当某个线程要等待另一个线程执行结束后才能继续执行时,使用join方法. public class DinnerThread { public static void main(Stri ...
- 使用matlab和ISE 创建并仿真ROM IP核
前言 本人想使用简单的中值滤波进行verilog相关算法的硬件实现,由于HDL设计软件不能直接处理图像,大部分过程都是可以将图像按照一定的顺序保存到TXT文档中,经过Modelsim仿真后,处理的数据 ...
- 解决Python2.7的UnicodeEncodeError: ‘ascii’ codec can’t encode异常错误
更改 sys.defaultencoding 为文件的编码方式 #! /usr/bin/env python # -*- coding: utf-8 -*- import sys reload ...