Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a C macro definition refer to other macros?

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!

like image 859
llakais Avatar asked Nov 01 '11 20:11

llakais


People also ask

Can we define macro inside a macro?

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.

Can you #define in a macro C?

Macros using #defineYou can define a macro in C using the #define preprocessor directive.

Can I #define in a macro?

(2) However, you cannot define a macro of a macro like #define INCLUDE #define STDH include <stdio. h> .

What is a macro definition in C?

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.


1 Answers

Short answer yes. You can nest defines and macros like that - as many levels as you want as long as it isn't recursive.

like image 60
Mysticial Avatar answered Sep 23 '22 19:09

Mysticial