How can the a operation be done in a C macro/preprocessor right after the substitution time, so not only substituting it but also finish all the math right before substituting it on real C code ?
Asked
Active
Viewed 242 times
0
-
2You question is not clear. Kindly write a clear question so that we can help you. Thanks – Saurav Rai Sep 27 '19 at 11:45
-
4Can you provide an example of what you hope to achieve? It's not clear to me how you would distinguish the behavior you want from the behavior you presently get. – John Bollinger Sep 27 '19 at 11:45
-
1Your updates so far clarify a little, but I'm still not seeing why you think there is a meaningful difference to be had. That is, the basic answer is that the preprocessor doesn't do that, but what sort of workaround would serve your particular case is unclear, as is whether you really need a workaround at all. – John Bollinger Sep 27 '19 at 11:51
-
3I think you should include some sample code in your question, showing us the difference between what you get and what you want. – Adrian Mole Sep 27 '19 at 11:52
-
@nicomp: Regarding your comment on the deleted answer, it is not impossible to do math in the format of the target CPU at compile time. I can do the math in the format of any CPU using just pencil and paper (if that paper includes a specification of the target CPU). And a compile can do the math as well. – Eric Postpischil Sep 27 '19 at 14:39
1 Answers
2
That's something that the preprocessor simply cannot do. But if the operation can (theoretically) be done at preprocessor time, then it can also be done at compile time, and the optimizer will take care of such things.
So a code snippet like this:
#define add(x,y) (x+y)
int x=add(4,5);
will produce this C code
int x=(4+5);
But any optimizer will be able to optimize the addition away during compile time. So the addition will not be present in the assembly code.
The preprocessor can do some arithmetics, but not after the substitution. For instance, this is ok:
#if 1 + 1 == 42
...
#endif

klutt
- 30,332
- 17
- 55
- 95