2010年12月26日 星期日

7 * 7 = 19

The output is 19, not 49.

#include <stdio.h>
#define square(x) (x * x)
int main()
{
  int i = 3;
  int j = 4;
  printf("%d\n", square(i + j));
}

1 則留言:

  1. Considering the rule "The macro expansion must always be parenthesized to protect any lower-precedence operators from the surrounding expression", square(x) is replaced by (x * x) which is (3 + 4 * 3 + 4). Correct definition one must be:

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

    回覆刪除