Strange behaviour of macros C/C++

前端 未结 5 600
再見小時候
再見小時候 2020-12-04 03:02

I\'m using some macros, and observing some strange behaviour.

I\'ve defined PI as a constant, and then used it in macros to convert degrees to radians and radians to

5条回答
  •  执笔经年
    2020-12-04 03:32

    Macros are (relatively simple) textual substitutions.

    Use parentheses in your definitions (both to enclose the macro itself and the macro arguments):

    #define PI (atan(1) * 4)
    #define radians(deg)  ((deg) * PI / 180)
    #define degrees(rad)  ((rad) * 180 / PI)
    

提交回复
热议问题