若干平方使用#define限定 [英] square of a number being defined using #define

查看:141
本文介绍了若干平方使用#define限定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是通过一定的code的这些常见的面试的准备。我想出了一些疑问,如果有人能帮助我对此?
我现在对这个完全糊涂了,

I was just going through certain code's which are frequently asked in Interview's. I came up with certain doubts, if anyone can help me regarding this? I am totally confused on this now,

#include<stdio.h>
#include<conio.h>
#define square(x) x*x

main()
{
      int i,j;
      i=4/square(4);
      j=64/square(4);
      printf("\n %d",i);
      printf("\n %d",j);
      printf("\n %d",square(4));
      getch();
}

输出是:

 4
 64
 16

我很纳闷,为什么平方(4) 1的回报,当我分吧?我的意思是我怎样才能获得的价值 4和64 当我把它而是直接使用时,我得到的 16 !?

I am wondering, why did square(4) return 1 when i divided it? i mean how can i get the value 4 and 64 when i divide it but when used directly i get 16!!?

推荐答案

平方正在-括号:它扩大文本上,这样

square is under-parenthesized: it expands textually, so

#define square(x) x*x
   ...
i=4/square(4);

办法

i=4/4*4;

这组为(4/4)* 4 。要修复,加上括号:

#define square(x) ((x)*(x))

仍然是一个非常玄乎的#define ,因为它计算 X 的两倍,因此平方( somefun())调用函数两次,做的的,因此一定计算一个正方形而是两个连续通话的产品,当然, - 。)

Still a very iffy #define as it evaluates x twice, so square(somefun()) calls the function twice and does not therefore necessarily compute a square but rather the product of the two successive calls, of course;-).

这篇关于若干平方使用#define限定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆