Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Multiline comments

Options
  • 09-03-2007 3:56pm
    #1
    Registered Users Posts: 801 ✭✭✭


    Here's something that I can't understand.

    If I have some code (I'm using C#, but this is common to any C style language) like so:
                  if (a == b)
    Brace 1 ->    {
                      if (a == c)
    Brace 2 ->        {
                          a = d;
    Brace 3 ->        }
    Brace 4 ->    }
    

    Here, Brace 1 will be matched with Brace 4 and Brace 2 will be matched with Brace 3. Just as expected!

    However, if I have:
    Comment 1 ->    /*
                      if (a == c)
    Comment 2 ->        /*
                          a = d;
    Comment 3 ->        */
    Comment 4 ->    */
    
    Comment 1 will be matched with Comment 3, Comment 2 won't match with anything, and Comment 4 will cause a compiler error.

    Why don't multiline comments work in the same way as braces and parentheses??? This annoys me when I'm debugging!!


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Its because Comment 2 is a part of Comment 1 since it is enclosed in it.

    Even if Comment 4 was above Comment 3, they would still be within the initial matching braces of Comment 1 and Comment 3, and so wouldn't actually need to be comment tags at all.


  • Registered Users Posts: 1,481 ✭✭✭satchmo


    Use #if 0 and #endif instead, they'll stack the way you want them to.


  • Registered Users Posts: 37,485 ✭✭✭✭Khannie


    This should be down to the compiler and its interpretation. The default interpretation is as aidan pointed out, and I quite like it that way personally.

    The MS compiler has an option to behave as you expect (or at least, I'm pretty sure it did in VC++ 6.0).


  • Closed Accounts Posts: 2,349 ✭✭✭nobodythere


    A multiline comment is opened by /* and closed by */. There's no point in programming a comment inside a comment so it ignores the second /*


  • Registered Users Posts: 1,481 ✭✭✭satchmo


    Khannie wrote:
    This should be down to the compiler and its interpretation.
    According to the standard, /* */ comments don't nest so you shouldn't rely on a particular compiler's different interpretation for any sort of cross-platform code.
    I quite like it that way personally
    Yeah I like it the way it is too, it makes sense - you don't have to go hunting for matching close-comments the way you do with braces.


  • Advertisement
  • Moderators, Recreation & Hobbies Moderators, Science, Health & Environment Moderators, Technology & Internet Moderators Posts: 91,693 Mod ✭✭✭✭Capt'n Midnight


    Nature Boy wrote:
    [php]
    if (a == b)
    Brace 1 -> {
    if (a == c)
    Brace 2 -> {
    a = d;
    Brace 3 -> }
    Brace 4 -> }
    [/php]


    [php]
    Comment 1 -> /*
    if (a == c)
    Comment 2 -> /*
    a = d;
    Comment 3 -> */
    Comment 4 -> */
    [/php] !
    php tags give some colour - but like the others have said the rules for continuing on the next line vary. If you use /* */ on each line it should always work - not so pretty unless you match the right hand side too.


  • Registered Users Posts: 801 ✭✭✭Nature Boy


    Thanks for the replies. The reason I'm asking is that if I've got code with a multiline comment somewhere and I want to comment out the entire block of code for debugging reasons, I have to take out all of the multiline comments and replace them with single lines, which can be a pain! Most other things can be nested, why not comments??


  • Moderators, Recreation & Hobbies Moderators, Science, Health & Environment Moderators, Technology & Internet Moderators Posts: 91,693 Mod ✭✭✭✭Capt'n Midnight


    Aren't there compiler options you can use to get the compiler to miss sections ?


  • Closed Accounts Posts: 16 ReallyDeadThing


    Nature Boy wrote:
    if I've got code with a multiline comment somewhere and I want to comment out the entire block of code for debugging reasons, I have to take out all of the multiline comments and replace them with single lines, which can be a pain!

    I find that annoying too. One handy solution I've started using is to replace the /* and */ in the inner comment with //. For example:
    /*
      if (a == c)
        //
          a = d;
        //
    */
    

    The // acts as a reminder to put the proper comment block back in once the whole thing is uncommented.


Advertisement