枚举 vs. const vs. #define 在 C/C++ 中
在这里,我们会看到在 C 或 C++ 程序中枚举、常量和 #define 之间有什么区别。在需要做出选择时,这三者可能会带来困惑。现在让我们看看这三者是什么。
const 或 static const
const 是常量类型数据,或者 static const 是常量,但存储规范符为 static。所以它将保持活动状态直到程序终止,且常量类型数据无法更新。
示例
#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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP