Using macro results in incorrect output when used as part of a larger math expression - why does this happen?

前端 未结 13 2126
别那么骄傲
别那么骄傲 2020-12-11 17:50

This is a normal C routine program which i found out in some question bank. It is shown below:

#define CUBE(p) p*p*p

main()
{
    int k;
    k = 27 / CUBE(3         


        
13条回答
  •  盖世英雄少女心
    2020-12-11 18:19

    When you do macros, you have to be careful about how you place parentheses. In this case, you don't have any, so the expression becomes 27/3*3*3, which by the precedence rules of / and * becomes (27/3)*3*3.

提交回复
热议问题