// Comment after backslash in #define causes compile errors
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-04-27 6:09 AM - last edited on ‎2025-04-28 12:55 AM by Andrew Neil
I had the same issue, looked for it on the internet, didn't get an answer, and I finally solved it right now:
You cannot have ANYTHING after a backslash \ . For example, I defined mine like so:
#define MY_MACRO(args) \ // The argument 'arg' does this thing - INVALID COMMENT
some_action(args) // Here I do this, this, and this - COMPLETELY VALID HERE (the macro doesn't continue furthermore -> macro continuation is not violated)
You cannot even put comments after this backslash (at least in C99 that I'm working on), because it breaks the macro continuation and the compiler gets confused over this, and then sees the next line ('some_action(args)') as some floating-in-the-air function call.
Note: Though, as I showed in the example above, you can freely have a comment on the very last line of the macro.
Solved! Go to Solution.
- Labels:
-
Other software
-
Other software tools
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-04-28 12:40 AM - edited ‎2025-04-28 12:55 AM
This makes sense, if you think about it:
Normally, a #define ends at the end of the source line.
The backslash overrides that - it says that more of the definition follows on the next line
So what you wrote:
#define MY_MACRO(args) \ // The argument 'arg' does this thing - INVALID COMMENT
some_action(args) // Here I do this, this, and this - COMPLETELY VALID HERE (the macro doesn't continue furthermore -> macro continuation is not violated)
Is equivalent to:
#define MY_MACRO(args) // The argument 'arg' does this thing - INVALID COMMENT some_action(args) // Here I do this, this, and this - COMPLETELY VALID HERE (the macro doesn't continue furthermore -> macro continuation is not violated)
(ie, the whole definition on one line)
So now it's clear that you have, in fact, commented-out the whole of your substitute text!
This also makes clear why it works at the end of the definition.
It should be OK if you use /* ... */ comments
PS:
Which reminded me that the backslash here is called the line-splicing operator.
A complex system designed from scratch never works and cannot be patched up to make it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-04-28 12:40 AM - edited ‎2025-04-28 12:55 AM
This makes sense, if you think about it:
Normally, a #define ends at the end of the source line.
The backslash overrides that - it says that more of the definition follows on the next line
So what you wrote:
#define MY_MACRO(args) \ // The argument 'arg' does this thing - INVALID COMMENT
some_action(args) // Here I do this, this, and this - COMPLETELY VALID HERE (the macro doesn't continue furthermore -> macro continuation is not violated)
Is equivalent to:
#define MY_MACRO(args) // The argument 'arg' does this thing - INVALID COMMENT some_action(args) // Here I do this, this, and this - COMPLETELY VALID HERE (the macro doesn't continue furthermore -> macro continuation is not violated)
(ie, the whole definition on one line)
So now it's clear that you have, in fact, commented-out the whole of your substitute text!
This also makes clear why it works at the end of the definition.
It should be OK if you use /* ... */ comments
PS:
Which reminded me that the backslash here is called the line-splicing operator.
A complex system designed from scratch never works and cannot be patched up to make it work.
