Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

c - Using Parentheses in Define Preprocessor Statements

So I was wondering when is to use

 #define xxx (yyy)

vs

 #define xxx  yyy

My project includes a files that has its own defines such at AD0_ADMD_CT if I wanted to redefine them would I need to use (AD0_ADMD_CT) or just AD0_ADMD_CT in the define or not?

AD0_ADMD_CT is a defined as

 #define    AD1_ADMD_CT     (IO_AD1.ADMD.bit.CT)

So it would be either

#define AD0_COMPARETIME     (AD0_ADMD_CT)

or

#define AD0_COMPARETIME     AD0_ADMD_CT
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...