Is it possible to define a constant such as foo.bar via
#define foo.bar 42
When I try the above foo is expanded to .bar 42. Is there any way to escape the period or otherwise work around this?
No . is not allowed in macro names since they are identifiers and identifiers are not allowed to include a .. We can see this by going to draft C99 standard section 6.10 Preprocessing directives which includes the following grammar:
# define identifier replacement-list new-line
# define identifier lparen identifier-listopt ) replacement-list new-line
# define identifier lparen ... ) replacement-list new-line
# define identifier lparen identifier-list , ... ) replacement-list new-line
^^^^^^^^^^
and section 6.4.2 Identifiers covers what is a valid identifier.
No, the period character is not permitted in C preprocessor macro names.
However, in this case you can achieve a similar effect without using the preprocessor:
static struct { int bar; } foo = {42};
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