Problem with Macros

后端 未结 4 1080
轻奢々
轻奢々 2020-12-06 13:40

HI ,

Can some one help me in understanding why the value of SQUARE(x) is 49 ?

I am using Visual C++ 6.0 .

#define SQUARE(X) X * X

int main(i         


        
4条回答
  •  佛祖请我去吃肉
    2020-12-06 14:07

    Macros are not functions: they just alter the text of the program. This operation is called preprocessing and it's automatically executed before your code gets compiled. People write macros to save their time and introduce some variability to their source code.

    When you write SQUARE(x), no actual funciton call happens, just the text is modified. The operation is quite dumb, so you have to do additional precautions in cases like yours. Refer to other answers for explanation of your case.

提交回复
热议问题