1. 程式人生 > >const 將變數宣告為常量

const 將變數宣告為常量

 const  type-name cinstant -name 

將變數宣告為常量

#include<iostream>

int main()
{
	using namespace std;

	const int Pi = 22.0 / 7;
	cout << "the value of constant Pi is " << Pi << endl;

	return 0;

}

輸出:

the value of constant Pi is 3.14286

const double 生命Pi是一個double型別的常量,變成const int 便是一個int 型的常量,不會再被賦值和修改。常量是一種確保不會被修改的強大方式。