I think it's better illustrated with an example. With a struct like this:
typedef struct {
int a;
char b;
} Foo;
Define a macro:
#define Foo(A, B) (Foo){ A, B }
Which can then be used like this:
Foo foo;
foo = Foo(1, 'c');
I've been using this pattern for a while, and for me it's a very elegant way of initializing structs. It works in GCC and Clang (and TCC I think), but in some lesser-known C compilers (like Plan 9's C compiler) it doesn't.
What does the C standard dictates about that topic?
Excerpt from §6.10.3.10 of the C11 standard:
Each subsequent instance of the function-like macro name followed by a ( as the next preprocessing token introduces the sequence of preprocessing tokens that is replaced by the replacement list in the definition (an invocation of the macro).
So, no, you should not worry. A function-like macro is only replaced if the macro name is followed by an opening parenthesis (.
The preprocessor does text substitution, it doesn't know anything about the C language. Thus, it doesn't care about C identifiers.
So, no, a macros cannot collide with an identifier.
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