Guille
Guille

Reputation: 430

Use of parenthesis in c

What is the difference of the 2 following statements, regarding the use of parenthesis? (No pointers or so)

#define UART1_BAUD (460800)
#define UART2_BAUD 9600

Upvotes: 1

Views: 275

Answers (1)

pmg
pmg

Reputation: 108978

There is no difference when the macros are used normally as operands in expressions.

Note however, there is a difference in

#define A 4 + 7
#define B (5 + 3)

if you use the macros as

int a = 6 * A; // 6 * 4 + 7 ==> 24 + 7
int b = 6 * B; // 6 * (5 + 3) ==> 6 * 8

As a rule of thumb: use, and abuse, parenthesis in macros.

When parenthesis immediately follow the macro name, it's a function-like macro

#define SQUARE(BAR) ((BAR) * (BAR)) // use and abuse parenthesis

Upvotes: 5

Related Questions