Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

75%
+4 −0
Q&A When should I parenthesize macro arguments in C?

Simply put: parenthesis are used whenever we suspect that there may be operator precedence issues. Either because the user passed an expression containing several operands and operators to the...

posted 6mo ago by Lundin‭

Answer
#1: Initial revision by user avatar Lundin‭ · 2023-11-13T14:39:58Z (6 months ago)
Simply put: parenthesis are used whenever we suspect that there may be operator precedence issues. 

- Either because the user passed an expression containing several operands and operators to the macro. 

  To deal with this we need to surround the use of each macro parameter with parenthesis. 
- Or because the macro is used in an expression together with other operators. 

  To deal with this, the function-like macro needs to be wrapped in an outer parenthesis.

And yes there exists plenty of situations where we don't really need to worry about what the user passed, since it wouldn't make a difference for the macro. Or if they pass something weird and therefore get a compiler error, then that's not necessarily a bad thing.

But it is good to keep ones coding style consistent and analyzable by tools. If we always follow both of the above mentioned best practices with parenthesis, then we can also verify, by means of a static analyzer tool, that no bugs caused by missing parenthesis exist.

It's kind of the same thing as writing `{}` after each `if`. While writing the code we can conclude that the braces after `if` aren't needed in some cases, yet we know that if we go lax with our coding style and start skipping them, that lax coding style will eventually lead to bugs.