使用 const 前缀声明指定类型的常量,const 类型的对象在程序执行期间不能被修改改变。如下所示:
const type variable = value;
#include <iostream>
using namespace std;
int main()
{
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
int area;
area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE;
return 0;
}
结果:
50
注意,把常量定义为大写字母形式.