By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,682 Members | 1,096 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,682 IT Pros & Developers. It's quick & easy.

Comments format: comments extending over multi-line

P: n/a
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

Nov 14 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"Monk" <sc******@gmail.com> 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
Nov 14 '05 #2

P: n/a
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

Nov 14 '05 #3

P: n/a
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.
Nov 14 '05 #4

P: n/a
"Monk" <sc******@gmail.com> 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) ks***@mib.org <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.
Nov 14 '05 #5

P: n/a
On Wed, 20 Apr 2005 08:50:09 GMT, Keith Thompson
<ks***@mib.org> 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
Nov 14 '05 #6

P: n/a
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.
Nov 14 '05 #7

P: n/a
Jason Curl <j_********@motorola.com> 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) ks***@mib.org <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.
Nov 14 '05 #8

P: n/a
Thanks a lot for the replies. They have been most helpful.

Cheers,
Sachin

Nov 14 '05 #9

P: n/a
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:Th*************@gmail.com>

Nov 14 '05 #10

P: n/a

In article <ln************@nuthaus.mib.org>, Keith Thompson <ks***@mib.org> writes:
Jason Curl <j_********@motorola.com> 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 mi************@microfocus.com

Then a good friend got very ill, and it made Pernsteiner realize that
"life is short. This was an opportunity to be silly."
Nov 14 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.