#include <stdio.h>
#define CUBE(x) (x * x * x)
int main() {
printf("%d", CUBE(4+5));
return 0;
}

- 131,814
- 10
- 121
- 189

- 37
- 4
-
1[The need for parentheses in macros in C](https://stackoverflow.com/q/10820340/995714) – phuclv Jan 20 '21 at 07:40
5 Answers
Here is how the macro is expanded during the compilation process:
printf("%d", (4+5 * 4+5 * 4+5));
Because *
has a higher precedence than +
, this expression is evaluated as (4 + (5 * 4) + (5 * 4) + 5)
, producing 49
instead of the expected 729
.
To avoid such operator precedence issues, all macro arguments must be parenthesised in the macro definition as well as the expression itself:
#define CUBE(x) ((x) * (x) * (x))
Note however that this expansion of CUBE
evaluates x
multiple times, which is a problem if the macro argument has side effects, such as CUBE(i++)
.
To avoid all these issues, use a function and let the compiler optimize it:
int cube(int x) {
return x * x * x;
}
You can add static inline
in front of this function definition, but modern optimizers will still inline the function without this.

- 131,814
- 10
- 121
- 189
Macro will turn the code to:
printf("%d", (4+5 * 4+5 * 4+5));
which is effectively:
printf("%d", 4 + (5*4) + (5*4) + 5); // 29
If you want to cube 9
to get 729
, you should write CUBE((4+5))
.
-
3Or stop using function-like macros. In today's world of optimising compilers, just write it as a function, the compiler will inline it if the benefits are enough (and you can even suggest that to the compiler with `inline`). – paxdiablo Jan 20 '21 at 07:33
-
3Re “ If you want 729, you should write CUBE((4+5))”: No, the macro should be written with parentheses so its user does not have to supply them. – Eric Postpischil Jan 20 '21 at 07:36
-
2@Eric, the amount of macro magic to cover *all* cases (such as `CUBE(x++)`, for example) make function-like macros a very bad idea. Better to just ditch them, use macros for simple definitions, and inline-suggested functions for everything else. – paxdiablo Jan 20 '21 at 07:38
-
1@paxdiablo: That is irrelevant to my comment. Further, C programmers must understand how macros work even if another one is never written in new code. Advising a person to use inline functions instead is okay for a comment or supplemental information in an answer but does not answer the question asked. – Eric Postpischil Jan 20 '21 at 08:03
-
More commonly, this is taken care of in the macro itself: `#define CUBE(x) ((x)*(x)*(x))` – paddy Jan 20 '21 at 08:30
-
@Eric, your comment presented a *better* way of doing it but still nowhere *near* fool-proof, as per my given example. I'm not advocating not *learning* about function-like macros since, yes, you will be called upon to fix the inevitable problems with them :-). I'm just stating that they probably shouldn't be used, preferring *actual* functions. Then a vast swathe of problems just go away. – paxdiablo Jan 20 '21 at 10:00
-
In any case, there's ample precedent on SO for answering "How do I X?" with "X'ing is a very bad idea, you're better off Y'ing instead" :-) – paxdiablo Jan 20 '21 at 10:01
you need to give the input like CUBE((4+5))
if you want to add the numbers and then send to CUBE
. Cause th CUBE(4+5)
is basically expanded to 4+5*4+5*4+5
as it sets the whole 4+5
in the place of x
. So, 4+5*4+5*4+5
= 4+20+20+5
as multiplication comes first and then adding them will give 49
.
Define the macro like #define CUBE(x) ((x)*(x)*(x))
it actually first proceed the (4+5)
operation for every (x)
and then do the *
operation.
Another way is just use CUBE((4+5))
while calling the macro, it basically first added the two numbers and (4+5)
= 9
and then do CUBE(9)
, like:
#include<stdio.h>
#define CUBE(x) (x * x * x)
int main( )
{
printf("%d", CUBE((4+5)));
return 0;
}

- 3,583
- 2
- 12
- 19
The C preprocessor will literally substitute all instances of x for 4+5, resulting in the following code:
i = 4+5*4+5*4+5;
(multiplication first, then addition)

- 125
- 2
- 22
Read Modern C, then the n1570 draft C standard, and the documentation of GCC and of GDB and of CPP. See also this C reference website.
Take inspiration from existing open source projects, e.g. on github, or GNU software.
With a recent GCC compiler, invoke it as gcc -Wall -Wextra -g
. Use also the -C -E
options to get the preprocessed form.
You could also be interested by the statement-expr extension of GCC.
Your CUBE(4+5)
is macro-expanded to 4+5*4+5*4+5
which is computed as 4+(5*4)+(5*4)+5
according to the precedence of C operators.
Consider coding a static inline
function like
static inline int cube(int x) { return x*x*x; }
or if you need a macro, at least
#define CUBE(X) ((X)*(X)*(X))
which won't work nicely for CUBE(i++)
(while with the inline
function, cube(i++)
does what you want it to do: increment i
once!).
BTW, you could use GPP, or GNU m4, (or your own generator, or GNU bison) to generate some C code. As a rule of thumb, think with ASTs: when you generate C code, emit a lot of useless parenthesis, like e.g. in Chicken Scheme, SWIG, or in CAIA or in my manydl.c
You could also use the Clang static analyzer on your code (and maybe Frama-C).
You may be interested in reading some coding guidelines like MISRA C or GNU ones.

- 223,805
- 18
- 296
- 547