Comments format: comments extending over multi-line

Discussion in 'C Programming' started by Monk, Apr 20, 2005.

  1. Monk

    Monk Guest

    Hi,

    Have a query regarding comments that extend over multiple-lines. Would
    like to know if the standard's view of this, so that we can create a
    code which doesn't run into compiler specific issues.

    1. A normal comments is
    /* comment text */

    2. A multiple line comment is
    /* comment text <new line>
    more comment text <new line>
    .... <many new lines>
    comment text */

    1 is fairly common. Does the standard say anything about format 2 ? I
    suspect different compilers might interpret 2 differently so the
    question.

    Any opinions or views on this issue are also welcome.

    Sachin
    Monk, Apr 20, 2005
    #1
    1. Advertising

  2. Monk

    Richard Bos Guest

    "Monk" <> wrote:

    > 1. A normal comments is
    > /* comment text */
    >
    > 2. A multiple line comment is
    > /* comment text <new line>
    > more comment text <new line>
    > ... <many new lines>
    > comment text */
    >
    > 1 is fairly common. Does the standard say anything about format 2 ?


    Yes. It is legal.

    From n869.txt, 6.4.9:
    # [#1] Except within a character constant, a string literal,
    # or a comment, the characters /* introduce a comment. The
    # contents of a comment are examined only to identify
    # multibyte characters and to find the characters */ that
    # terminate it.

    Note: _only_ for mb characters and */. Anything else, including
    newlines, is ignored.

    Richard
    Richard Bos, Apr 20, 2005
    #2
    1. Advertising

  3. Monk wrote:
    > Hi,
    >
    > Have a query regarding comments that extend over multiple-lines. Would
    > like to know if the standard's view of this, so that we can create a
    > code which doesn't run into compiler specific issues.
    >
    > 1. A normal comments is
    > /* comment text */
    >
    > 2. A multiple line comment is
    > /* comment text <new line>
    > more comment text <new line>
    > .... <many new lines>
    > comment text */
    >
    > 1 is fairly common. Does the standard say anything about format 2 ? I


    Are you joking? format 2 is widely used.

    Krishanu

    > suspect different compilers might interpret 2 differently so the
    > question.
    >
    > Any opinions or views on this issue are also welcome.
    >
    > Sachin
    >
    Krishanu Debnath, Apr 20, 2005
    #3
  4. Monk

    Flash Gordon Guest

    Monk wrote:
    > Hi,
    >
    > Have a query regarding comments that extend over multiple-lines. Would
    > like to know if the standard's view of this, so that we can create a
    > code which doesn't run into compiler specific issues.
    >
    > 1. A normal comments is
    > /* comment text */
    >
    > 2. A multiple line comment is
    > /* comment text <new line>
    > more comment text <new line>
    > ... <many new lines>
    > comment text */
    >
    > 1 is fairly common. Does the standard say anything about format 2 ? I
    > suspect different compilers might interpret 2 differently so the
    > question.
    >
    > Any opinions or views on this issue are also welcome.


    Your C text book should tell you that format 2 is completely standard.
    You can have as many new lines as you want arranged however you want
    between the /* and */
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Apr 20, 2005
    #4
  5. "Monk" <> writes:
    > Have a query regarding comments that extend over multiple-lines. Would
    > like to know if the standard's view of this, so that we can create a
    > code which doesn't run into compiler specific issues.
    >
    > 1. A normal comments is
    > /* comment text */
    >
    > 2. A multiple line comment is
    > /* comment text <new line>
    > more comment text <new line>
    > ... <many new lines>
    > comment text */


    Both forms are perfectly legal; any compiler that breaks on either of
    them is badly broken and should not be trusted.

    To stray a bit from the question, if I write a multi-line comment
    block I generally try to mark each line:

    /*
    * Comment line 1
    * Comment line 2
    * Comment line 3
    */

    but that's just a matter of visual style. The extra '*' characters
    are simply part of the comment.

    Note also that comments don't nest; a "/*" within a comment has no
    special meaning:

    /* This is /* just a single comment */
    /* This is /* not */ a nested comment */

    On the second line, the comment ends with the first "*/", and what
    follows is probably a syntax error.

    Again, any compiler that gets this wrong should not be trusted.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Apr 20, 2005
    #5
  6. On Wed, 20 Apr 2005 08:50:09 GMT, Keith Thompson
    <> wrote:

    > To stray a bit from the question, if I write a multi-line comment
    > block I generally try to mark each line:
    >
    > /*
    > * Comment line 1
    > * Comment line 2
    > * Comment line 3
    > */
    >
    > but that's just a matter of visual style. The extra '*' characters
    > are simply part of the comment.


    My preferred editor (vim) puts those in automatically in C mode (by
    default, it can be turned off if the "house style" is different but
    that's the way I prefer it).

    > Note also that comments don't nest; a "/*" within a comment has no
    > special meaning:
    >
    > /* This is /* just a single comment */
    > /* This is /* not */ a nested comment */
    >
    > On the second line, the comment ends with the first "*/", and what
    > follows is probably a syntax error.


    Some compilers warn about putting /* inside a comment, because a lot of
    the time when people do that they are used to (or porting code from)
    non-standard compilers which allow nested comments (as I recall some
    compilers have an option to allow nested comments for compatibility
    with old code).

    Chris C
    Chris Croughton, Apr 20, 2005
    #6
  7. Monk

    Jason Curl Guest

    Monk wrote:
    > Hi,
    >
    > Have a query regarding comments that extend over multiple-lines. Would
    > like to know if the standard's view of this, so that we can create a
    > code which doesn't run into compiler specific issues.
    >
    > 1. A normal comments is
    > /* comment text */
    >
    > 2. A multiple line comment is
    > /* comment text <new line>
    > more comment text <new line>
    > .... <many new lines>
    > comment text */
    >
    > 1 is fairly common. Does the standard say anything about format 2 ? I
    > suspect different compilers might interpret 2 differently so the
    > question.
    >
    > Any opinions or views on this issue are also welcome.
    >
    > Sachin
    >

    Both 1. and 2. are allowed.

    What you will find differences on are nested comments, e.g.

    1 /* Comment 1
    2 /* Nested comment */
    3 */

    Some compilers will see a nested comment and see that line 3 contains
    the end of the comment, other compilers will see line 2 ending the
    comment and fail compilation on line 3.

    I don't know if nested comments are part of the standard.
    Jason Curl, Apr 20, 2005
    #7
  8. Jason Curl <> writes:
    [...]
    > What you will find differences on are nested comments, e.g.
    >
    > 1 /* Comment 1
    > 2 /* Nested comment */
    > 3 */
    >
    > Some compilers will see a nested comment and see that line 3 contains
    > the end of the comment, other compilers will see line 2 ending the
    > comment and fail compilation on line 3.
    >
    > I don't know if nested comments are part of the standard.


    No, nested comments are not part of the standard. Since some code
    actually depends on a "/*" within a comment being ignored, a compiler
    that allowed nested comments (at least in its default mode) would
    cause problems.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Apr 20, 2005
    #8
  9. Monk

    Monk Guest

    Thanks a lot for the replies. They have been most helpful.

    Cheers,
    Sachin
    Monk, Apr 20, 2005
    #9
  10. Chris Croughton wrote:
    >
    > On Wed, 20 Apr 2005 08:50:09 GMT, Keith Thompson

    [...]
    > > Note also that comments don't nest; a "/*" within a comment has no
    > > special meaning:
    > >
    > > /* This is /* just a single comment */
    > > /* This is /* not */ a nested comment */
    > >
    > > On the second line, the comment ends with the first "*/", and what
    > > follows is probably a syntax error.

    >
    > Some compilers warn about putting /* inside a comment, because a lot of
    > the time when people do that they are used to (or porting code from)
    > non-standard compilers which allow nested comments (as I recall some
    > compilers have an option to allow nested comments for compatibility
    > with old code).


    I think that's mostly from people commenting out blocks of lines with /* */
    and ending up with:

    /* comment this out

    some old code;
    some more old code; /* a comment */
    even more old code;

    end of comment */


    On the other hand, I vaguely remember a long time ago a compiler which was
    broken enough to treat "/*" within a string literal as a begin-comment.
    This, of course, did nasty things with constructs like:

    sprintf(namebuf,"%s/*.*",path);

    On the other other hand, my memory isn't what it used to be. ;-)

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Apr 20, 2005
    #10
  11. In article <>, Keith Thompson <> writes:
    > Jason Curl <> writes:
    > >
    > > Some compilers will see a nested comment and see that line 3 contains
    > > the end of the comment, other compilers will see line 2 ending the
    > > comment and fail compilation on line 3.

    >
    > No, nested comments are not part of the standard. Since some code
    > actually depends on a "/*" within a comment being ignored, a compiler
    > that allowed nested comments (at least in its default mode) would
    > cause problems.


    For example, I've worked with at least one programmer who liked to put
    a comment at the end of nearly every line, and liked to comment out
    code by inserting "/*" at the beginning of the line, thus:

    foo(x); /* do something */
    /* bar(x); /* do something else */
    /* baz(x); /* and yet another thing */
    return x;

    A compiler that "allowed nested comments" (ie, failed to process
    comments as mandated by the C language) would fail to compile this
    correctly.

    Now, this is a poor practice, for a number of reasons; for example,
    if this programmer had put /* at the beginning of the final line
    above, the comment would have extended until a */ was found further
    down in the source file. It's much better to use #if / #endif to
    suppress interpretation of a section of code. However, there is C
    code which uses it, and so "supporting" nested comments would be a
    Bad Thing.

    --
    Michael Wojcik

    Then a good friend got very ill, and it made Pernsteiner realize that
    "life is short. This was an opportunity to be silly."
    Michael Wojcik, Apr 20, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cesar Zam
    Replies:
    0
    Views:
    337
    Cesar Zam
    Nov 6, 2003
  2. Trond Valen
    Replies:
    5
    Views:
    395
    Niklas Norrthon
    Dec 7, 2005
  3. dean
    Replies:
    5
    Views:
    460
    Phlip
    Jun 4, 2006
  4. bernadou

    Force multi line field value to output with line breaks?

    bernadou, Jan 22, 2006, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    358
    bernadou
    Jan 23, 2006
  5. Artur Merke

    multi line comments in ruby

    Artur Merke, Nov 10, 2003, in forum: Ruby
    Replies:
    5
    Views:
    201
    Josef 'Jupp' SCHUGT
    Nov 17, 2003
Loading...

Share This Page