Strange behavior of Macro-expansion

前端 未结 3 1130
悲&欢浪女
悲&欢浪女 2020-12-12 01:47

Here\'s the code:

#include 
#include 
#define VAL1(a,b)    a*b
#define VAL2(a,b)    a/b
#define VAL3(a,b)    ++a%b
int main()
{         


        
3条回答
  •  感动是毒
    2020-12-12 02:12

    I tried to compile it with GCC 4.7, and the two expressions gave the same results.

    If you want to see how macro will expand, you can use cpp, which is the C Preprossessor, and will give you the output after preprocessor expression are executed, here the output is

    int main()
    {
        int a = 1;
        int b = 2;
        int c = 3;
        int d = 3;
        int e = 5;
    
        int result = a/d/e*b+++c%d;  
        int result2 = a/d/e*b+++c%d;  
    
        printf("%d %d\n", result, result2);
    
        return 0;
    }
    

提交回复
热议问题