Comments format: comments extending over multi-line

M

Monk

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
 
R

Richard Bos

Monk said:
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
 
K

Krishanu Debnath

Monk said:
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
 
F

Flash Gordon

Monk said:
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 */
 
K

Keith Thompson

Monk said:
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.
 
C

Chris Croughton

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
 
J

Jason Curl

Monk said:
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.
 
K

Keith Thompson

Jason Curl said:
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.
 
K

Kenneth Brody

Chris said:
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:[email protected]>
 
M

Michael Wojcik

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top