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