Why would someone use #define to define constants?(为什么有人会使用#define 来定义常量?)
问题描述
这是一个简单的问题,但为什么有人会使用#define 来定义常量?
It's simple question but why would someone use #define to define constants?
有什么区别
#define sum 1
和 const int sum = 1;
推荐答案
#define
有许多不同的应用程序,但您的问题似乎是关于一个特定的应用程序:定义命名常量.
#define
has many different applications, but your question seems to be about one specific application: defining named constants.
在 C++ 中,很少有理由使用 #define
来定义命名常量.
In C++ there's rarely a reason to use #define
to define named constants.
#define
通常在 C 代码中广泛使用,因为 C 语言在定义常量时与 C++ 有很大不同.简而言之,const int
对象在 C 中不是 constants,这意味着在 C 中定义真正常量的主要方法是使用 #define
.(另外,对于 int
常量,可以使用枚举).
#define
is normally widely used in C code, since C language is significantly different from C++ when it comes to defining constants. In short, const int
objects are not constants in C, which means that in C the primary way to define a true constant is to use #define
. (Also, for int
constants one can use enums).
这篇关于为什么有人会使用#define 来定义常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么有人会使用#define 来定义常量?


基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 初始化列表*参数*评估顺序 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05