Can a C macro definition refer to other macros?

2019-02-04 00:45发布

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!

5条回答
可以哭但决不认输i
2楼-- · 2019-02-04 01:19

The answer is "yes", and two other people have correctly said so.

As for why the answer is yes, the gory details are in the C standard, section 6.10.3.4, "Rescanning and further replacement". The OP might not benefit from this, but others might be interested.

6.10.3.4 Rescanning and further replacement

After all parameters in the replacement list have been substituted and # and ## processing has taken place, all placemarker preprocessing tokens are removed. Then, the resulting preprocessing token sequence is rescanned, along with all subsequent preprocessing tokens of the source file, for more macro names to replace.

If the name of the macro being replaced is found during this scan of the replacement list (not including the rest of the source file's preprocessing tokens), it is not replaced. Furthermore, if any nested replacements encounter the name of the macro being replaced, it is not replaced. These nonreplaced macro name preprocessing tokens are no longer available for further replacement even if they are later (re)examined in contexts in which that macro name preprocessing token would otherwise have been replaced.

The resulting completely macro-replaced preprocessing token sequence is not processed as a preprocessing directive even if it resembles one, but all pragma unary operator expressions within it are then processed as specified in 6.10.9 below.

查看更多
看我几分像从前
3楼-- · 2019-02-04 01:20

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

查看更多
等我变得足够好
4楼-- · 2019-02-04 01:22

I'd like to add a gotcha that tripped me up.

Function-style macros cannot do this.

Example that doesn't compile when used:

#define FOO 1
#define FSMACRO(x) FOO + x
查看更多
淡お忘
5楼-- · 2019-02-04 01:24

Yes, that is supported. And used quite a lot!

One important thing to note though is to make sure you paranthesize the expression otherwise you might run into nasty issues!

#define MEH FOO/BAR

// vs

#define MEH (FOO / BAR)

// the first could be expanded in an expression like 5 * MEH to mean something 
//   completely different than the second
查看更多
Fickle 薄情
6楼-- · 2019-02-04 01:26

Yes, it's going to work.


But for your personal information, here are some simplified rules about macros that might help you (it's out of scope, but will probably help you in the future). I'll try to keep it as simple as possible.

  • The defines are "defined" in the order they are included/read. That means that you cannot use a define that wasn't defined previously.

  • Usefull pre-processor keyword: #define, #undef, #else, #elif, #ifdef, #ifndef, #if

  • You can use any other previously #define in your macro. They will be expanded. (like in your question)

  • Function macro definitions accept two special operators (# and ##)

operator # stringize the argument:

#define str(x) #x
str(test); // would translate to "test"

operator ## concatenates two arguments

#define concat(a,b) a ## b
concat(hello, world); // would translate to "helloworld"

There are some predefined macros (from the language) as well that you can use:

__LINE__, __FILE__, __cplusplus, etc

See your compiler section on that to have an extensive list since it's not "cross platform"

  • Pay attention to the macro expansion

You'll see that people uses a log of round brackets "()" when defining macros. The reason is that when you call a macro, it's expanded "as is"

#define mult(a, b) a * b
mult(1+2, 3+4); // will be expanded like: 1 + 2 * 3 + 4 = 11 instead of 21.
mult_fix(a, b) ((a) * (b))
查看更多
登录 后发表回答