Skip to main content
Explorer
April 27, 2025
Solved

// Comment after backslash in #define causes compile errors

  • April 27, 2025
  • 1 reply
  • 576 views

Split from: 
https://community.st.com/t5/stm32-mcus-embedded-software/expected-declaration-specifiers-or-before/td-p/649225


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.

    This topic has been closed for replies.
    Best answer by Andrew Neil

    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:

    https://stackoverflow.com/questions/30133528/comments-in-c-macro-definition/30133750#:~:text=Naturally%20you%20can%27t%20use%20the%20C99%20comment%20style%20with%20//%2C%20since%20it%20would%20ignore%20the%20rest%20of%20the%20line%20and%20prevent%20you%20from%20creating%20a%20multi%2Dline%20macro

     

    https://complete-concrete-concise.com/programming/c/how-to-add-comments-to-macros/#:~:text=Even%20if%20the%20macro%20is%20spread%20over%20several%20lines%20using%20the%20line%20splicing%20(%5C)%20operator%2C%20the%20preprocessor%20turns%20it%20into%20a%20single%20line%20and%20everything%20following%20the%20//%20will%20be%20considered%20part%20of%20the%20comment%20(hence%2C%20white%20space)

    Which reminded me that the backslash here is called the line-splicing operator.

    1 reply

    Super User
    April 28, 2025

    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:

    https://stackoverflow.com/questions/30133528/comments-in-c-macro-definition/30133750#:~:text=Naturally%20you%20can%27t%20use%20the%20C99%20comment%20style%20with%20//%2C%20since%20it%20would%20ignore%20the%20rest%20of%20the%20line%20and%20prevent%20you%20from%20creating%20a%20multi%2Dline%20macro

     

    https://complete-concrete-concise.com/programming/c/how-to-add-comments-to-macros/#:~:text=Even%20if%20the%20macro%20is%20spread%20over%20several%20lines%20using%20the%20line%20splicing%20(%5C)%20operator%2C%20the%20preprocessor%20turns%20it%20into%20a%20single%20line%20and%20everything%20following%20the%20//%20will%20be%20considered%20part%20of%20the%20comment%20(hence%2C%20white%20space)

    Which reminded me that the backslash here is called the line-splicing operator.