#include<stdlib.h>
#include<string>
#include<math.h>
#include<iostream>
#define PI 3.1415926
using namespace std;
int main()
{
double s = 0, r, c;
cout<<"输入半径 r = ";
cin>>r;
#ifdef PI //如果定义了宏 PI ,执行程序段1 若改为#ifndef PI
{
s = PI * r * r;
c = 2 * PI * r;
cout<<"s = "<<s<<endl;
cout<<"c = "<<c<<endl;
}
#else //如果没有定义宏则执行程序段2
{
s = 3.14 * r * r;
c = 2 * 3.14 * r;
cout<<"s = "<<s<<endl;
cout<<"c = "<<c<<endl;
}
#endif //最后要有结束的标志,否则无法正常结束!
system("pause");
}
总结:1.#ifdef是如果定义了标示符才执行,程序段1
2.#ifndef是没定义标示符才执行,程序段1
3.#if 如果标示符 为真(非0)执行程序段1
4.前三者最后都得加上#endif才行!