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

前端 未结 13 1983
别那么骄傲
别那么骄傲 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:27
         #define CUBE(p) p*p*p
         main()
         {
            int k;
            k=27/CUBE(3);
            printf("%d",k);
    
         }
    

    As per my understanding and knowledge the value of K should be 1 as CUBE(3) would be replaced by 3*3*3 during preprocessing

    YES

    and after the subsequent compilation it would be giving the value of 1,but instead it has shown the value of 81 which has made me curious to know how it happenned.

    NO,

    k= 27/3*3*3 
    
     =(((27/3)*3)*3) (The precedence of `*` and `/` are same but the associativity is from left to right)
     =((9*3)*3) =81
    

    Replace #define CUBE(p) p*p*p with #define CUBE(p) ((p)*(p)*(p))

    0 讨论(0)
  • 2020-12-11 18:33

    Your macro is not protected. Try

    #define CUBE(p) ((p)*(p)*(p))
    

    The current macro was expanded to

    k=27/3*3*3
    

    which is ((27/3)*3)*3

    0 讨论(0)
  • 2020-12-11 18:35

    Because of operator precedence 27/3*3*3 = 81

    You could use instead:

    inline int cube(int p) { return p*p*p; }
    
    0 讨论(0)
  • 2020-12-11 18:35

    Because macros are a textual substitution, that works out to:

    k = 27 / 3 * 3 * 3;
    

    Since multiplication and division happen left to right, that works out to:

    k = ((27 / 3) * 3) * 3;
    

    So, you want to change that in two ways:

    #define CUBE(p) ((p)*(p)*(p))
    

    The outer parentheses cause the multiplications to be done before any other operations.

    The parentheses around the individual p's are for the case where you do:

    CUBE(1 + 2);
    

    Without those inner parentheses, operator precedence will trip you up.

    0 讨论(0)
  • 2020-12-11 18:37

    C macros do textual substitution (i.e. it's equivalent to copying and pasting code). So your code goes from:

    k=27/CUBE(3);
    

    to

    k=27/3*3*3;
    

    Division and multiplication have the same precedence and have left-to-right associativity, so this is parsed as:

    k=((27/3)*3)*3;
    

    which is 9 * 3 * 3 = 81.

    This is why C macros should always be defined with liberal use of parentheses:

    #define CUBE(p) ((p) * (p) * (p))
    

    For more information, see http://c-faq.com/cpp/safemacros.html from the comp.lang.c FAQ.

    0 讨论(0)
  • 2020-12-11 18:37

    Hi answer for this is:81 Explanation: In step k=27/cube(3) cube(3) is replaced by 3*3*3 by preprocessor.then above statement becomes as k=27/3*3*3 in that 27/3 expression is evaluated by c compiler(operator precedence) the result is(27/3) :9 the statement k=27/3*3*3 becomes as k=9*3*3; the result for above statement is 81:

    0 讨论(0)
提交回复
热议问题