当前位置:网站首页>The difference between enumeration and define macro
The difference between enumeration and define macro
2022-07-06 22:31:00 【It's Beichen not too PI acridine】
Enumeration and #define The difference between macros
How to use enumeration types
The general definition is as follows :
enum enum_type_name
{
ENUM_CONST_1,
ENUM_CONST_2,
...
ENUM_CONST_n
} enum_variable_name;
enum_type_name Is a custom data type name , and enum_variable_name by enum_type_name A variable of type , That is, we often say enumerating variables . actually enum_type_name A type is a restriction on the value range of a variable , In curly brackets is the range of its value , namely enum_type_name Variable of type enum_variable_name It can only take any value in curly brackets , If the value assigned to the variable of this type is not in the list , An error or warning will be reported .ENUM_CONST_1、ENUM_CONST_2、… 、ENUM_CONST_n, These members are constants , That is what we usually call enumeration constants ( Constants are usually capitalized ).
enum Variable types can also assign values to constant symbols in them , If you do not assign a value, you will add... From the constant assigned with the initial value 1, If there is no assignment , Their values range from 0 Start increasing in sequence 1. For example, use a constant to represent different colors :
enum Color
{
GREEN = 1,
RED,
BLUE,
GREEN_RED = 10,
GREEN_BLUE
}ColorVal;
The values represented by each constant name are :
GREEN = 1
RED = 2
BLUE = 3
GREEN_RED = 10
GREEN_BLUE = 11
Enumeration and #define The difference between macros
1.#define Macro constants are simply replaced in the precompile phase . Enumeration constants determine their values at compile time .
2. Usually in compilers , You can debug enumeration constants , But you cannot debug macro constants .
3. Enumeration can define a large number of related constants at one time , and #define A macro can only define one at a time .
1. Enumeration can do ,#define Macro can do it all ? If you can , So why do we need enumeration ?
answer : Not all of them . On the one hand, enumeration can centrally manage data , Integer data with the same attributes can be stored by enumeration ; In addition, enumeration can realize “ Self increment of value ”( Of course, you can also specify the value of each enumeration ), It's easier to write code , Reduce the chance of mistakes , If you need to add a new information in subsequent maintenance, you do not need to calculate the value of this information .
2.sizeof(ColorVal) What is the value of ? Why? ?
answer : The value is 4,ColorVal An enumeration variable , And the enumeration variable represents an integer
Enumerate variables , Variables defined by enumeration types . Enumeration variable size , That is, the memory occupied by enumeration types . Due to the assignment of enumeration variables , Only one constant in the enumeration structure can be stored at a time . So enumerate the size of variables , The essence is the size of the memory space occupied by the constant ( The constant is int type , The current mainstream compiler is generally 32 Bit machine and 64 In the machine int All of them are 4 Bytes ), The same is true for the memory size of enumeration types .
边栏推荐
- 【编译原理】做了一半的LR(0)分析器
- 在IPv6中 链路本地地址的优势
- 硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
- pytorch_ Yolox pruning [with code]
- Learn the principle of database kernel from Oracle log parsing
- 手写ABA遇到的坑
- Aardio - 通过变量名将变量值整合到一串文本中
- Seata聚合 AT、TCC、SAGA 、 XA事务模式打造一站式的分布式事务解决方案
- Sword finger offer question brushing record 1
- Aardio - 利用customPlus库+plus构造一个多按钮组件
猜你喜欢

MySQL数据库基本操作-DML

Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem

自定义 swap 函数

CocosCreator+TypeScripts自己写一个对象池

Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"

Config:invalid signature solution and troubleshooting details

第3章:类的加载过程(类的生命周期)详解

Aardio - 利用customPlus库+plus构造一个多按钮组件

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
![[linear algebra] determinant of order 1.3 n](/img/6e/54f3a994fc4c2c10c1036bee6715e8.gif)
[linear algebra] determinant of order 1.3 n
随机推荐
UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop
MySQL教程的天花板,收藏好,慢慢看
Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
uniapp设置背景图效果demo(整理)
rust知识思维导图xmind
云原生技术--- 容器知识点
Lora sync word settings
每日一题:力扣:225:用队列实现栈
(十八)LCD1602实验
雅思口语的具体步骤和时间安排是什么样的?
Classification, function and usage of MySQL constraints
MySQL----初识MySQL
2022-07-05 使用tpcc对stonedb进行子查询测试
signed、unsigned关键字
Assembly and interface technology experiment 5-8259 interrupt experiment
Aardio - 利用customPlus库+plus构造一个多按钮组件
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
Gd32f4xx serial port receive interrupt and idle interrupt configuration
Web APIs DOM 时间对象
剪映+json解析将视频中的声音转换成文本