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. URL组成成分及各部分作用简介及urllib.parse / uri

    URL的一般格式为(带方括号[]的为可选项): protocol :// hostname[:port] / path / [;parameters][?query]#fragment urllib. ...

  2. Git超实用总结

    Git 是什么? Git 是一个分布式的代码管理容器,本地和远端都保有一份相同的代码. Git 仓库主要是由是三部分组成:本地代码,缓存区,提交历史,这几乎是所有操作的本质,但是为了文章更加简单易懂, ...

  3. CentOS7安装配置Bacula yum方法

    参考: https://www.baidu.com/link?url=o2QIy2YZWjsJPAFJuYFhrH3nPvtyRkSe-o5Q_FqFZ5E1EMOsIOmGeKm0HAonwHOw8 ...

  4. 在linux执行kettle

    1.下载 最新版下载 7.1 https://community.hitachivantara.com/docs/DOC-1009855 2.准备 3.上传任务文件 .kjb,.ktr 4.上传mys ...

  5. SharePoint Framework 企业向导(四)

    博客地址:http://blog.csdn.net/FoxDave 接上一讲 嵌入JavaScript脚本 开发者常常使用的比较受欢迎的开发方式是嵌入JavaScript脚本,也叫JavaScri ...

  6. leetcode第27题:移除指定元素

    给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...

  7. Ubuntu配置静态IP

    1. 输入命令:sudo vi /etc/network/interfaces 编辑文件: auto lo iface lo inet loopback auto eth0 iface eth0 in ...

  8. python基础题型一

    一.执行Python脚本的两种方式 #python name.py   Python解析器 #chmod +x name.py 二.简述位.字节的关系 1Byte=8bit 三.简述ascii.uni ...

  9. C#窗体换肤

    Form1.cs using System;using System.Collections.Generic;using System.ComponentModel;using System.Data ...

  10. 多进程于多线程的区别,cpu密集型适合用什么

    多线程:在单个程序中同事运行多少个线程完成不同的工作,成为线程. 线程共享内存空间,进程的内存是独立的, 同一个进程的线程间可以直接交流: 两个进程想通信,必须通过一个中间代理来实现, 一个线程可以控 ...