posts - 12,  comments - 21,  trackbacks - 0
#include<iostream>
using namespace std;
#define PI 
3.1416
#define AREA(R) PI
*R*R
#deifne CUBE(X) ((X)
*(X)*(X))

double Area(double r)
{
   
double t;
   
const double pi=3.1416;
   t
=pi*r*r;
   
return t;
}

double Cube(double s)
{
   
double q;
   q
=s*s*s;
   
return q;
}

int main()
{
   cout
<<"AREA(R)="<<AREA(3)<<endl;
   cout
<<"Area(r)="<<Area(3)<<endl;
   
int height=100;
   cout
<<"CUBE(X)="<<CUBE(height++)<<endl;
   cout
<<"height="<<height<<endl;
   height
=10;
   cout
<<"Cube(s)="<<Cube(height++)<<endl;
   cout
<<"height="<<height<<endl;
   
return 0;
}
为什么上述代码height两次输出不一样;
第一次height输出为103,第二次为11
posted on 2011-06-01 20:44 DoubleW 阅读(467) 评论(3)  编辑 收藏 引用

FeedBack:
# re: C++中const和C的#define区别
2011-06-01 21:37 | K.V
CUBE是宏,CUBE(height++)相当于((height++)*(height++)*(height++))
height 自增了三次,当然是103
Cube是函数,Cube(height++)执行时height传值进去,然后自增一次,自然为11
  回复  更多评论
  
# re: C++中const和C的#define区别
2011-06-02 08:48 | lwch
#define是预编译指令,在编译时只起到了替换的作用。
const则是在堆上创建了一个对象。  回复  更多评论
  
# re: C++中const和C的#define区别
2011-06-02 11:48 | DoubleW
哦哦 原来这样啊!!!谢谢点拨..  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2011年6月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

常用链接

留言簿(3)

随笔档案(12)

文章档案(2)

最新随笔

搜索

  •  

积分与排名

  • 积分 - 28138
  • 排名 - 660

最新随笔

最新评论

阅读排行榜

评论排行榜