Thread: Nested comments don't work?

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Nested comments don't work?

    I was wondering why multiline C-style comments don't work?

    Code:
    /* BEGIN_COMMENT1
      /* BEGIN_COMMENT2
      */ END_COMMENT2
    */ END_COMMENT1
    The reason I'm asking is, I want to comment out some code in the constructor of my class using the multiline comments, but I had already written some multiline comments as description headers for different things inside the area I want to comment, so I can't just simply wrap it with the multiline comments, and be done with it, because that doesn't work. And its a pain to have to go through the whole function again just to change some comment styles (the function is some 70,000+ lines of code), so is there another way to do this??

    Thanks in advance.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Nope they don't work in anything I've ever tried, and there are no plans to change that afaik.
    Just put a #if 0 #endif around the code you want to temporarily disable.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Since /* ... */ are C-style comment, you should be using the C++ style // comments for your code.

    Then when you want to comment out a section, the /* ... */ style will work much better.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by iMalc View Post
    Nope they don't work in anything I've ever tried, and there are no plans to change that afaik.
    Just put a #if 0 #endif around the code you want to temporarily disable.
    Well, I tried that already after reading about it on the web, but the example that they showed was around the function declaration, not inside of a function definition, which is where I need it.
    So I'm guessing that doing it inside a function definition is not legal, since doing that produced a whole mess of compiler errors... EDIT: Nevermind...maybe its legal, its just causing other problems.
    Quote Originally Posted by WaltP View Post
    Since /* ... */ are C-style comment, you should be using the C++ style // comments for your code.

    Then when you want to comment out a section, the /* ... */ style will work much better.
    Well, I'm actually using both. I used // above less-important sections of code, while /* and */ above more important or bigger sections of code, so that, for instance, I could differentiate the attributes code from the Html tags code, since attribute code is inside Html tag code, and each attribute is a separate entity within the tag. But, anyway, I suppose I could have used ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// in place of that instead, but I didn't know starting out that nesting multiline comments is not allowed.

    So now I guess I'll have to write a short little program to do the tedious task of changing comment styles for me, so I wont have to do it all by hand....
    Last edited by Programmer_P; 09-23-2012 at 03:22 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    C comments don't nest. I am pretty sure it's in the grammar section of the standard, so you won't find a tool where this works.

    Not sure how you screwed up the #if 0 suggestion. Try below:

    So now I guess I'll have to write a short little program to do the tedious task of changing comment styles for me, so I wont have to do it all by hand....
    Not a terrible idea but: Surround the code you want to exclude in an #ifdef...#endif then define the macro when you want it again with a compiler switch or something.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Another way to "comment out" a section of code is to:

    Code:
    if (false)
    {
        // code that will not be executed
    }
    It's not exactly the same as commenting it out, because it'll still be parsed and syntax-checked, but in a pinch it works.

    To disable an if-statement, you can add "&& false" to the condition:

    Code:
    if (false && condition)
    {
        // disabled
    }
    The #if 0...#endif is pretty universal, though. If that didn't work for you, chances are your program doesn't operate correctly with that region of code removed.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Comments
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 03-30-2010, 01:04 PM
  2. What the... nested fpout doesnt work?
    By rkooij in forum C Programming
    Replies: 18
    Last Post: 05-14-2007, 07:09 AM
  3. Need some comments
    By shaizy in forum C Programming
    Replies: 0
    Last Post: 05-26-2006, 01:59 AM
  4. comments
    By nightingale in forum C Programming
    Replies: 32
    Last Post: 07-18-2003, 03:49 AM
  5. comments
    By ZeeeD in forum C Programming
    Replies: 3
    Last Post: 10-04-2001, 11:33 AM