c++定义常量两种方式(#define 和const)

定义常量

在 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;
}

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章