定義常量
在 C++ 中,有兩種簡單的定義常量的方式:
- 使用 #define 預處理器。
- 使用 const 關鍵字。
第一種#define定義
#include <iostream>
using namespace std;
#define PI 3.14; /*#define 定義*/
int main()
{
float p=PI;
cout <<p<< endl;
return 0;
}
第二種const定義
#include <iostream>
using namespace std;
int main()
{
const float PI=3.14; /*const定義*/
cout <<PI<< endl;
return 0;
}