为什么有人使用#define来定义常量? [英] Why would someone use #define to define constants?
问题描述
这是一个简单的问题,但为什么有人用#define来定义常量?
之间有什么区别
#define sum 1
和 const int sum = 1;
在C ++中,很少有使用 #define
定义命名常量的原因。
#define
通常广泛用于C代码,因为C语言与C ++有很大不同定义常量。简而言之, #define
。 (另外,对于 int
常量,可以使用枚举)。
It's simple question but why would someone use #define to define constants?
What's the difference between
#define sum 1
and const int sum = 1;
#define
has many different applications, but your question seems to be about one specific application: defining named constants.
In C++ there's rarely a reason to use #define
to define named constants.
#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来定义常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!