Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
With macros, you are better off using parens at any time you use one of the parameters in an expression context. The problem with operator precedence is not limited to + and *. Did you know that c...
Answer
#2: Post edited
- With macros, you are better off using parens at any time you use one of the parameters in an _expression context._
- The problem with operator precedence is not limited to + and *. Did you know that comma (`,`) is an operator? Did you know that comma has a *lower* precedence than assignment? Did you know that assignment is considered an operator in C?
- Did you know that comma is also used to declare multiple variables of the same type?
- What does the expression `x = a, b` do? What about `int x = a, b;` ?
- In general, if you are using macro parameters as "values", you should go ahead and parenthesize them. It generally doesn't hurt, and it might help.
- Only if you are using macro parameters as *text* or *code* or *strings* should you (maybe) not parenthesize them. That is, if you are using a macro parameter to construct a name, like:
- #define unique_name(base) base ## __COUNTER__
- then don't use parens, because you want the "raw" value of `base` (and because it wouldn't make sense with parens). Likewise if you are stringizing the parameter:
- #define DO_PRAGMA(x) _Pragma (#x)
then don't use parens, because you don't want parens in the string you are creating. (Or, if you do want them, then go ahead!) And finally if the parameter is itself code, not an expression:- #define DO_ONCE(code, ...) do code, ## __VA_ARGS__ while(0)
then don't use parens because they are likely to be invalid syntax. Maybe use braces `{...}` instead.
- With macros, you are better off using parens at any time you use one of the parameters in an _expression context._
- The problem with operator precedence is not limited to + and *. Did you know that comma (`,`) is an operator? Did you know that comma has a *lower* precedence than assignment? Did you know that assignment is considered an operator in C?
- Did you know that comma is also used to declare multiple variables of the same type?
- What does the expression `x = a, b` do? What about `int x = a, b;` ?
- In general, if you are using macro parameters as "values", you should go ahead and parenthesize them. It generally doesn't hurt, and it might help.
- Only if you are using macro parameters as *text* or *code* or *strings* should you (maybe) not parenthesize them. That is, if you are using a macro parameter to construct a name, like:
- #define unique_name(base) base ## __COUNTER__
- then don't use parens, because you want the "raw" value of `base` (and because it wouldn't make sense with parens). Likewise if you are stringizing the parameter:
- #define DO_PRAGMA(x) _Pragma (#x)
- then don't use parens, because you don't want parens in the string you are creating. (Or, if you do want them, then go ahead!) Also, parens interfere with string literal concatenation. So it's generally better to leave stringized expressions bare. And finally if the parameter is itself code, not an expression:
- #define DO_ONCE(code, ...) do code, ## __VA_ARGS__ while(0)
- then don't use parens because they are likely to be invalid syntax. Maybe use braces `{...}` instead. (But really that's a horrible macro that I just made up to make a point. Don't do things like that in real life!)
#1: Initial revision
With macros, you are better off using parens at any time you use one of the parameters in an _expression context._ The problem with operator precedence is not limited to + and *. Did you know that comma (`,`) is an operator? Did you know that comma has a *lower* precedence than assignment? Did you know that assignment is considered an operator in C? Did you know that comma is also used to declare multiple variables of the same type? What does the expression `x = a, b` do? What about `int x = a, b;` ? In general, if you are using macro parameters as "values", you should go ahead and parenthesize them. It generally doesn't hurt, and it might help. Only if you are using macro parameters as *text* or *code* or *strings* should you (maybe) not parenthesize them. That is, if you are using a macro parameter to construct a name, like: #define unique_name(base) base ## __COUNTER__ then don't use parens, because you want the "raw" value of `base` (and because it wouldn't make sense with parens). Likewise if you are stringizing the parameter: #define DO_PRAGMA(x) _Pragma (#x) then don't use parens, because you don't want parens in the string you are creating. (Or, if you do want them, then go ahead!) And finally if the parameter is itself code, not an expression: #define DO_ONCE(code, ...) do code, ## __VA_ARGS__ while(0) then don't use parens because they are likely to be invalid syntax. Maybe use braces `{...}` instead.