Using Parentheses in Define Preprocessor Statements

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.

Leave a Comment