#include <iostream>
using namespace std;
#define PI 3.14159;
void main()
{
const int R =10;
double p,s;
p = 2*R*PI;
s = R*R*PI;
cout<<"半径:"<<R<<endl;
cout<<"周长:"<<p<<endl;
cout<<"面积:"<<s<<endl;
}
用编译预处理命令#define和关键字const定义符号变量为什么会有不同的结果
#include <iostream>
using namespace std;
const PI=3.14159;
void main()
{
const int R =10;
double p,s;
p = 2*R*PI;
s = R*R*PI;
cout<<"半径:"<<R<<endl;
cout<<"周长:"<<p<<endl;
cout<<"面积:"<<s<<endl;
}
用 #define PI 3.14159; 输出的R=10,P=62.8318,S =341.159
用 const PI=3.14159; 输出的R=10,P=2.00858e+009,S=1.45298e+009
为什么会这样呢
#include
using namespace std;
const PI=3.14159;
void main()
{
const int R =10;
double p,s;
p = 2*R*PI;
s = R*R*PI;
cout<<"半径:"<<R<<endl;
cout<<"周长:"<<p<<endl;
cout<<"面积:"<<s<<endl;
}
用 #define PI 3.14159; 输出的R=10,P=62.8318,S =341.159
用 const PI=3.14159; 输出的R=10,P=2.00858e+009,S=1.45298e+009
为什么会这样呢
楼上,const与#define不同,#define定义量不需要指定数据类型,但是const定义常量需要指定数据类型。因此你的程序中:const PI=3.14159应该是const double PI=3.14159