What I'm trying to figure out is if something such as this (written in C):
#define FOO 15 #define BAR 23 #define MEH (FOO / BAR)
is allowed? I would want the preprocessor to replace every instance of
MEH
with
(15 / 23)
but I'm not so sure that will work. Certainly if the preprocessor only goes through the code once then I don't think it'd work out the way I'd like.
I found several similar examples but all were really too complicated for me to understand. If someone could help me out with this simple one I'd be eternally grateful!
No, this is not possible. During translation, all preprocessing directives ( #define , #include , etc.) are executed before any macro expansion occurs, so if a macro expands into a preprocessing directive, it won't be interpreted as such - it will be interpreted as (invalid) source code.
Macros using #defineYou can define a macro in C using the #define preprocessor directive.
(2) However, you cannot define a macro of a macro like #define INCLUDE #define STDH include <stdio. h> .
In C, A macro is any constant value or variable with its value. And the macro name will get replaced by its value in the entire program. Macros help in writing less code and also in saving time.
Short answer yes. You can nest defines and macros like that - as many levels as you want as long as it isn't recursive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With