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
I've seen macros use parentheses to enclose its arguments. Most of these make sense, as in #define sum(a, b) ((a) + (b)) The outer prevents the following: #define sum_bad(a, b) (a) + (b) ...
#4: Post edited
When should I parenthesize my macro arguments in C?
- When should I parenthesize macro arguments in C?
#3: Post edited
- I've seen macros use parentheses to enclose its arguments. Most of these make sense, as in
- ```c
- #define sum(a, b) ((a) + (b))
- ```
- The outer prevents the following:
- ```c
- #define sum_bad(a, b) (a) + (b)
- s = sum_bad(x, y) * z; // (x) + (y) * z
- ```
- The inner prevents the following:
- ```c
- #define mul_bad(a, b) (a * b)
- m = mul_bad(x, y + z); // (x * y + z)
- ```
- ----
- However, I've seen some parentheses that don't seem justified.
- ```c
- #define foo(a, b) bar((a), (b)) // Why not just `bar(a, b)`
- ```
- ```c
- #define asd(a, b) do \
- {
- int x = (b); // Why not just `= b;`?
- zxc(x);
}- ```
- Is it really necessary to always enclose macro arguments in parentheses, or is it superfluous in some cases?
- I've seen macros use parentheses to enclose its arguments. Most of these make sense, as in
- ```c
- #define sum(a, b) ((a) + (b))
- ```
- The outer prevents the following:
- ```c
- #define sum_bad(a, b) (a) + (b)
- s = sum_bad(x, y) * z; // (x) + (y) * z
- ```
- The inner prevents the following:
- ```c
- #define mul_bad(a, b) (a * b)
- m = mul_bad(x, y + z); // (x * y + z)
- ```
- ----
- However, I've seen some parentheses that don't seem justified.
- ```c
- #define foo(a, b) bar((a), (b)) // Why not just `bar(a, b)`
- ```
- ```c
- #define asd(a, b) do \
- {
- int x = (b); // Why not just `= b;`?
- zxc(x);
- } while (0)
- ```
- Is it really necessary to always enclose macro arguments in parentheses, or is it superfluous in some cases?
#2: Post edited
- I've seen macros use parentheses to enclose its arguments. Most of these make sense, as in
- ```c
- #define sum(a, b) ((a) + (b))
- ```
The outer prevent the following:- ```c
- #define sum_bad(a, b) (a) + (b)
- s = sum_bad(x, y) * z; // (x) + (y) * z
- ```
- The inner prevents the following:
- ```c
- #define mul_bad(a, b) (a * b)
- m = mul_bad(x, y + z); // (x * y + z)
- ```
- ----
- However, I've seen some parentheses that don't seem justified.
- ```c
- #define foo(a, b) bar((a), (b)) // Why not just `bar(a, b)`
- ```
- ```c
- #define asd(a, b) do \
- {
- int x = (b); // Why not just `= b;`?
- zxc(x);
- }
- ```
- Is it really necessary to always enclose macro arguments in parentheses, or is it superfluous in some cases?
- I've seen macros use parentheses to enclose its arguments. Most of these make sense, as in
- ```c
- #define sum(a, b) ((a) + (b))
- ```
- The outer prevents the following:
- ```c
- #define sum_bad(a, b) (a) + (b)
- s = sum_bad(x, y) * z; // (x) + (y) * z
- ```
- The inner prevents the following:
- ```c
- #define mul_bad(a, b) (a * b)
- m = mul_bad(x, y + z); // (x * y + z)
- ```
- ----
- However, I've seen some parentheses that don't seem justified.
- ```c
- #define foo(a, b) bar((a), (b)) // Why not just `bar(a, b)`
- ```
- ```c
- #define asd(a, b) do \
- {
- int x = (b); // Why not just `= b;`?
- zxc(x);
- }
- ```
- Is it really necessary to always enclose macro arguments in parentheses, or is it superfluous in some cases?
#1: Initial revision
When should I parenthesize my macro arguments in C?
I've seen macros use parentheses to enclose its arguments. Most of these make sense, as in ```c #define sum(a, b) ((a) + (b)) ``` The outer prevent the following: ```c #define sum_bad(a, b) (a) + (b) s = sum_bad(x, y) * z; // (x) + (y) * z ``` The inner prevents the following: ```c #define mul_bad(a, b) (a * b) m = mul_bad(x, y + z); // (x * y + z) ``` ---- However, I've seen some parentheses that don't seem justified. ```c #define foo(a, b) bar((a), (b)) // Why not just `bar(a, b)` ``` ```c #define asd(a, b) do \ { int x = (b); // Why not just `= b;`? zxc(x); } ``` Is it really necessary to always enclose macro arguments in parentheses, or is it superfluous in some cases?