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的更多相关文章

  1. EC笔记,第一部分:2.尽量以const,enum,inline代替#define

    02.尽量以const,enum,inline代替#define 原因:编译前的预处理会替换宏,所以调试的时候找不到错误 1.const 尽量用const替代常量宏定义 两种特殊情况: (1).常量指 ...

  2. Effective C++ 之 Item 2:尽量以 const, enum, inline 替换 #define

    Effective C++ Chapter 1. 让自己习惯C++(Accustoming Yourself to C++) Item 2. 尽量以 const, enum, inline 替换 #d ...

  3. 条款2:尽量以const、enum、inline替换#define

    1> 以const替换#define • 比如用const double Ratio = 1.653替换#define RATIO 1.653 因为宏定义在预处理阶段就会被替换成其所指代的内容, ...

  4. C++学习之路(七):以const,enum,inline替换#define

    这篇博文主要是编程中的一些问题和技巧.如题目所示,这些关键字的作用不再进行描述.直接描述功能和实例代码. 首先,在头文件中对类进行定义,是不会为类分配内存空间的,在这一点上类定义可以和普通变量类型的声 ...

  5. 【Effective C++ 读书笔记】条款02: 尽量以 const, enum, inline 替换 #define

    条款02: 尽量以 const, enum, inline 替换 #define 这个条款或许可以改为“宁可以编译器替换预处理器”. 编译过程: .c文件--预处理-->.i文件--编译--&g ...

  6. iOS学习——#define、const、typedef的区别

    在iOS开发中经常遇到一些字段和类型的定义,例如配置生产和测试不同环境的参数等,这时候经常用到#define.const以及typedef.那么它们之间有什么区别呢?我们接下来一个一个具体了解下. 一 ...

  7. c++中typedef、define、const、inline之间的区别

    1.typedef和#define的区别 typedef int* pInt; , b = ; const pInt p1 = &a; //p1是常量指针 pInt const p2 = &a ...

  8. #define、const、typedef的区别

    #define.const.typedef的区别 #define 并不是定义变量, 只是用来做文本替换 例如: #define PI 3.1415926 float angel; angel=30*P ...

  9. IOS 基础-define、const、extern、全局变量

    这里介绍一下define.const.extern的用法.优劣以及要注意的地方. 1.define 宏define是定义一个变量,没有类型信息.define定义的常量在内存中有若干个拷贝. defin ...

随机推荐

  1. jmeter4.0 源码编译 二次开发

    准备: 1.jmeter4.0源码 - apache-jmeter-4.0_src.zip 2.IDE Eclipse - Oxygen.3 Release (4.7.3) 3.JDK - 1.8.0 ...

  2. bootstrapTable 学习使用

    Bootstrap离线API Bootstrap Table 离线API <input type="button" id="btn_searcher" v ...

  3. Android开发 ---xml布局元素

    1.android:orientation="vertical/horizontal" vertical为垂直布局, horizontal为水平布局 2.android:layou ...

  4. GNU C的定义长度为0的数组

    在标准C和C++中,长度为0的数组是被禁止使用的.不过在GNU C中,存在一个非常奇怪的用法,那就是长度为0的数组,比如Array[0];很多人可能觉得不可思议,长度为0的数组是没有什么意义的,不过在 ...

  5. 很Low的三级菜单程序

    # -*-coding:utf-8-*- # Author:sunhao province={ '广东省':{ '深圳市':['南山区','龙岗区','福田区'], '广州市':['荔湾区','海珠区 ...

  6. L275 Climate Change Is Having a Major Impact on Global Health

    Climate Change Is Having a Major Impact on Global Health A devastating heat wave swept across Europe ...

  7. [MyBatis] MyBatis理论入门

    什么是MyBatis iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAOs) 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射. MyB ...

  8. winform 子窗体调用父窗体中的方法

    在父窗体里定义委托 public delegate void inis(string str); 在父窗体中定义要调用的方法 public void inigs(string gs) { textBo ...

  9. 集合(ArrayList)简述

    ArrayList创建变量的步骤 1.导入包java.util.ArrayList; 2.创建引用类型的变量 数据类型<集合存储的数据类型> 变量名=new 数据类型<集合存储的数据 ...

  10. Server SSL certificate verification failed: certificate has expired, issuer is not trusted

    Unable to connect to a repository at URL 'https://xxxxx/svn/include' Server SSL certificate verifica ...