I do not understand the behaviour of #define macro in C++

后端 未结 4 1329
既然无缘
既然无缘 2021-01-29 14:09

I need to understand how this code works:

#define foo1( a ) (a * a)              // How does this work?
inline int foo2(         


        
4条回答
  •  抹茶落季
    2021-01-29 14:55

    Macro is not a function. The compiler will expand all macros and then compile it. To see the expanded code, you can use the following command using -E option in gcc:

    gcc -E  -o 
    

    Or in Visual C++, under Configuration Properties->C/C++->Preprocessor, set "Generate Preprocessed File".

    BTW, your macro is problematic. You should use

    #define foo1( a ) ((a) * (a))
    

    instead of

     #define foo1( a ) (a * a)
    

提交回复
热议问题