C / C ++中的enum vs. const vs.#define
在这里,我们将看到C或C++程序中的enum,const和#define之间有什么区别。当我们必须决定选择它们时,这三者会造成一些混乱。现在让我们看看这三件事是什么。
const或静态const
const是常量类型数据,或者staticconst是常量,但存储说明符是静态的。因此它将保持活动状态,直到程序终止,并且常量类型数据无法更新。
示例
#include <iostream>
using namespace std;
main() {
int x;
x = 65700;
cout << "x is (as integer):" << x << endl;
x = (short)65700; //will be rounded after 2-bytes
cout << "x is (as short):" << x << endl;
}输出结果
x is (as integer):65700 x is (as short):164