Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include period in macro

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?

like image 566
vandale Avatar asked Oct 22 '25 22:10

vandale


2 Answers

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.

like image 200
Shafik Yaghmour Avatar answered Oct 25 '25 13:10

Shafik Yaghmour


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};
like image 25
Greg Hewgill Avatar answered Oct 25 '25 13:10

Greg Hewgill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!