Using Parentheses in Define Preprocessor Statements

前端 未结 4 1633
灰色年华
灰色年华 2020-12-06 13:59

So I was wondering when is to use

 #define xxx (yyy)

vs

 #define xxx  yyy

My project includes a files tha

4条回答
  •  伪装坚强ぢ
    2020-12-06 14:21

    There is no difference in both. In first case XXX is replaced by yyy and by (yyy) is second case. The convention to use brackts is to avoid logical errors that may occur. For example you define addition function as:

    #define f(N) N+N 
    int a = f(5)*f(5)  
    

    Expected value is 10*10 = 100 , but output is 35 because at compile time is becomes

    int a = 5+5*5+5, so using operator preference rule, output changes.

    So parenthesis avoid these type of errors.

提交回复
热议问题