What problems might the following macro bring to the application?

前端 未结 6 2051
伪装坚强ぢ
伪装坚强ぢ 2021-01-27 02:50

Can the following macro bring problems?

#define sq(x) x*x

If yes, then how and why?please help.

6条回答
  •  北荒
    北荒 (楼主)
    2021-01-27 03:35

    While writing macros use brackets excessively. Rewrite the macro as follows

    #define sq(x) ((x)*(x))
    

    If you don't do this then you will have problems in cases where macro is used as sq(5+4)

    To understand the problem do macro expansion and see.

提交回复
热议问题