#include<cstdio> //调用iostream库,否则使用printf和scanf语句编译出错
using namespace std; //这个程序可省略这行
const double PI=3.1415926; //PI是符号常量。代表3.1415926
int main()
{
float r,c,s; //定义实型变量
printf("r="); //显示提示符r=
scanf("%f",&r); //输入r的值,&符号不能漏掉
c=2*PI*r; //计算圆的周长
s=PI*r*r; //计算圆的面积
printf("c=%.2f s=%.2f\n",c,s); //显示计算结果,结果保留2位小数
}
程序中定义的PI代表常量3.1415926,在编译源程序时,遇到PI就用常量3.1415926代替,PI可以和常量一样进行运算。C++语言规定,每个符号常量的定义占据一个书写行,而且符号常量不能被再赋值。如果在例2.5中使用以下赋值语句是错误的。
PI=3.1415926;
#include<cstdio>
//#include<iostream>
using namespace std;
int main()
{
float r,c,s,pi;
pi=3.1415926;
printf("r=");
scanf("%f",&r);
c=2*pi*r;
s=pi*r*r;
printf("c=%.2f s=%.2f\n",c,s);
return 0;
}