niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C
// vs /*
Post new topic   Reply to topic Page 2 of 4 [60 Posts] View previous topic :: View next topic
Goto page:  Previous  1, 2, 3, 4 Next
Author Message
Joe Van Dyk
*nix forums addict


Joined: 06 Mar 2006
Posts: 96

PostPosted: Wed Jun 21, 2006 12:41 pm    Post subject: Re: // vs /* Reply with quote

Richard Heathfield wrote:
Quote:
Joe Van Dyk said:


Hi,

Is using // for a comment standard C?


Yes and no. Several reasons have been given for avoiding them in your C90
programs, but nobody appears to have mentioned the fact that modding down
ANSI conformance to allow them will, in some implementations, also
necessarily involve removing some other diagnostic messages. For example,
on the implementation I use most during development, enabling // comments
involves disabling a great many ANSI conformance checks. It's too high a
price.

Hm. What implementations might it hinder ANSI conformance checking?

Thanks,
Joe
Back to top
Christopher Benson-Manica
*nix forums Guru


Joined: 01 Apr 2005
Posts: 464

PostPosted: Wed Jun 21, 2006 1:05 pm    Post subject: Re: // vs /* Reply with quote

Joe Van Dyk <joe.vandyk@boeing.com> wrote:

Quote:
Hm. What implementations might it hinder ANSI conformance checking?

I'm fairly sure Richard uses gcc; passing -ansi to the compiler both
disables support for // comments and enables all sorts of other
conformance checks that, as Richard pointed out, are more or less
indispensable.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Back to top
Kenny McCormack
*nix forums Guru


Joined: 24 Mar 2005
Posts: 657

PostPosted: Wed Jun 21, 2006 2:10 pm    Post subject: Re: // vs /* Reply with quote

In article <44992c97.1114349855@news.xs4all.nl>,
Richard Bos <rlb@hoekstra-uitgeverij.nl> wrote:
Quote:
gazelle@xmission.xmission.com (Kenny McCormack) wrote:

In article <1150884078.651539.286840@g10g2000cwb.googlegroups.com>,
Nick Keighley <nick_keighley_nospam@hotmail.com> wrote:
...
gcc continues to support both a pre-processor which does not process //
comments, and one which doe

so what? All the world is not GCC.

I assume that if I pointed out that Nabisco makes at least 2 kinds of
Oreos, you'd "smartly" point out that "All the world is not Nabisco".

If you used it as an argument that Oreos are available everywhere, yes,
that would be a valid counterpoint.

And who but a paranoid freak would try to draw such a ridiculous
inference?
Back to top
Tom St Denis
*nix forums Guru Wannabe


Joined: 22 Feb 2005
Posts: 161

PostPosted: Wed Jun 21, 2006 2:21 pm    Post subject: Re: // vs /* Reply with quote

Joe Van Dyk wrote:
Quote:
Hi,

Is using // for a comment standard C?

Use /* */ comments.

Many CC's still don't support //. Mostly because they're C90
compilers.

So instead of going through your code and converting all /* */ to // or
/// just use

/** Doxy comment */

And save yourself a big f'ing headache.

Tom
Back to top
Ryan Reich
*nix forums beginner


Joined: 25 Apr 2005
Posts: 13

PostPosted: Wed Jun 21, 2006 3:40 pm    Post subject: Re: // vs /* Reply with quote

On Wed, 21 Jun 2006 06:05:19 GMT, Keith Thompson <kst-u@mib.org> wrote:
Quote:
Personally, I really like end-of-line comments ("//" in C99 and C++,
"#" in Perl and shell scripting, "--" in Ada). It's easy to comment
out large blocks of code given a decent editor, there's no ambiguity
about nested comments, and if you're looking at the middle of a large
comment block there's no question that it's in a comment block.

Nested comments are never ambiguous; only if you are really looking for
complication do they become ambiguous, and in that case, they're just as
ambiguous for // as for /* */. Consider:

/* A comment /* with a comment inside */
// A comment // with a comment inside

Syntactically, they're identical. The most obvious way of parsing comments
will not notice the nesting. How is it more ambiguous for the first whether
or not there's a syntax error? It's possible, even likely, that a human might
find the latter to be clearer, but who cares what humans think about how C
code is parsed? Only the compiler really cares, and the compiler is not
fooled by visual cues like which opening delimiter is closer to the closing
one, when the standard is that "the closing one" matches the _first_ opening
one, not the nearest.

Your first objection, about commenting out large blocks, is not really valid
since to do so with /* requires only two insertions, and you can even make
those on their own new lines, which requires no interference with the existing
text at all. It's actually easier to comment out large blocks this way than
with //.

To uncomment is another story, since text is line-based but /* */ is
non-local, but in practice you have either syntax highlighting, so the editor
knows this non-local information, or you follow a common style and put a * at
the beginning of each commented line, in which case for the purposes of
automation there is no difference between /* */ and //. Apropos of your third
comment, the only difference here between these two is now that for the
former, this is a convention, and for the latter, this is a standard.
Nonetheless, if you are using an editor to create large comments it will
enforce its own conventions consistently.

Quote:
But of course it's a matter of taste, and I don't expect to convince
anyone.

Of course not. For example, you wisely chose not to name the "decent editor"
with which one would be commenting out code.

--
Ryan Reich
ryan.reich@gmail.com
comp.lang.c
Back to top
Kenneth Brody
*nix forums Guru


Joined: 20 Apr 2005
Posts: 401

PostPosted: Wed Jun 21, 2006 5:41 pm    Post subject: Re: // vs /* Reply with quote

Ryan Reich wrote:
[...]
Quote:
Nested comments are never ambiguous; only if you are really looking for
complication do they become ambiguous, and in that case, they're just as
ambiguous for // as for /* */. Consider:

/* A comment /* with a comment inside */
// A comment // with a comment inside

Syntactically, they're identical. The most obvious way of parsing comments
will not notice the nesting. How is it more ambiguous for the first whether
or not there's a syntax error? It's possible, even likely, that a human might
find the latter to be clearer, but who cares what humans think about how C
code is parsed? Only the compiler really cares, and the compiler is not
fooled by visual cues like which opening delimiter is closer to the closing
one, when the standard is that "the closing one" matches the _first_ opening
one, not the nearest.

Actually, neither of those is a "nested comment". The first is a
comment with "/*" within it, and the second is a comment with "//"
in it.

Unless, that is, your compiler supports nested /*...*/ comments, in
which case the first example hasn't ended the comment.

/* This is a comment /* with a nested comment */ inside of it. */

Quote:
Your first objection, about commenting out large blocks, is not really valid
since to do so with /* requires only two insertions, and you can even make
those on their own new lines, which requires no interference with the existing
text at all. It's actually easier to comment out large blocks this way than
with //.

Consider this case where the compiler doesn't really "nest" /*...*/
comments. (Which is what you are assuming, given your first example.)

void foo()
{
... a bunch of code ...

/* the following code was commented out on 1-jan-2000 by xyz

foo
bar

i++; /* I added this, having not noticed that this is within
* commented-out code.
*/

bar
foo

*** end of 1-jan-2000 comment */

... some more code ...
}


[...]

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap@gmail.com>
Back to top
Ryan Reich
*nix forums beginner


Joined: 25 Apr 2005
Posts: 13

PostPosted: Wed Jun 21, 2006 6:19 pm    Post subject: Re: // vs /* Reply with quote

On Wed, 21 Jun 2006 13:41:53 -0400, Kenneth Brody <kenbrody@spamcop.net>
wrote:
Quote:
Ryan Reich wrote: [...]
Nested comments are never ambiguous; only if you are really looking for
complication do they become ambiguous, and in that case, they're just as
ambiguous for // as for /* */. Consider:

/* A comment /* with a comment inside */ // A comment // with a comment
inside

Syntactically, they're identical. The most obvious way of parsing comments
will not notice the nesting. How is it more ambiguous for the first
whether or not there's a syntax error? It's possible, even likely, that a
human might find the latter to be clearer, but who cares what humans think
about how C code is parsed? Only the compiler really cares, and the
compiler is not fooled by visual cues like which opening delimiter is
closer to the closing one, when the standard is that "the closing one"
matches the _first_ opening one, not the nearest.

Actually, neither of those is a "nested comment". The first is a comment
with "/*" within it, and the second is a comment with "//" in it.

Unless, that is, your compiler supports nested /*...*/ comments, in which
case the first example hasn't ended the comment.

/* This is a comment /* with a nested comment */ inside of it. */

Well, if the compiler nests comments then the first one is certainly a syntax
error, since then we have an unterminated comment. And my point is that the
second one should logically be considered a syntax error as well for the same
reason.

Interestingly, your example below breaks my theory if the compiler _doesn't_
nest comments, but if you make all the comments //, then it breaks if the
compiler _does_, since then many of the comments become unterminated. Of
course, no one ever writes a compiler that nests // comments. Perhaps that
was Keith's real point.

Quote:
Your first objection, about commenting out large blocks, is not really
valid since to do so with /* requires only two insertions, and you can even
make those on their own new lines, which requires no interference with the
existing text at all. It's actually easier to comment out large blocks
this way than with //.

Consider this case where the compiler doesn't really "nest" /*...*/
comments. (Which is what you are assuming, given your first example.)

void foo() { ... a bunch of code ...

/* the following code was commented out on 1-jan-2000 by xyz

foo bar

i++; /* I added this, having not noticed that this is within
* commented-out code.
*/

bar
foo

*** end of 1-jan-2000 comment */

... some more code ...
}


[...]


This, however, I was wrong about.

--
Ryan Reich
ryan.reich@gmail.com
comp.lang.c
Back to top
bill
*nix forums Guru Wannabe


Joined: 12 May 2005
Posts: 241

PostPosted: Wed Jun 21, 2006 8:11 pm    Post subject: Re: // vs /* Reply with quote

Kenneth Brody wrote:

Quote:
/* This is a comment /* with a nested comment */ inside of it. */


// And just for completeness // this is nested
while this line is part of the outer comment!!
Back to top
Keith Thompson
*nix forums Guru


Joined: 28 Feb 2005
Posts: 5173

PostPosted: Wed Jun 21, 2006 8:32 pm    Post subject: Re: // vs /* Reply with quote

rlb@hoekstra-uitgeverij.nl (Richard Bos) writes:
Quote:
gazelle@xmission.xmission.com (Kenny McCormack) wrote:
In article <1150884078.651539.286840@g10g2000cwb.googlegroups.com>,
Nick Keighley <nick_keighley_nospam@hotmail.com> wrote:
...
gcc continues to support both a pre-processor which does not process //
comments, and one which doe

so what? All the world is not GCC.

I assume that if I pointed out that Nabisco makes at least 2 kinds of
Oreos, you'd "smartly" point out that "All the world is not Nabisco".

If you used it as an argument that Oreos are available everywhere, yes,
that would be a valid counterpoint.

Please don't feed the troll.

--
Keith Thompson (The_Other_Keith) kst-u@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.
Back to top
Keith Thompson
*nix forums Guru


Joined: 28 Feb 2005
Posts: 5173

PostPosted: Wed Jun 21, 2006 8:46 pm    Post subject: Re: // vs /* Reply with quote

Ryan Reich <ryan.reich@gmail.com> writes:
Quote:
On Wed, 21 Jun 2006 06:05:19 GMT, Keith Thompson <kst-u@mib.org> wrote:
Personally, I really like end-of-line comments ("//" in C99 and C++,
"#" in Perl and shell scripting, "--" in Ada). It's easy to comment
out large blocks of code given a decent editor, there's no ambiguity
about nested comments, and if you're looking at the middle of a large
comment block there's no question that it's in a comment block.

Nested comments are never ambiguous; only if you are really looking for
complication do they become ambiguous, and in that case, they're just as
ambiguous for // as for /* */.

Of course there's no ambiguity as far as the compiler is concerned,
since the language has explicit rules that resolve any potential
ambiguity. In the case of /* */ comments, the rule is that a /*
sequence within a /* */ comment is ignored.

Of course, the language *could* have specified that nested comments
are significant, for example:

/* In a comment /* nested comment */ still in the outer comment */

But since comments are intended for the reader (the compiler is merely
responsible for ignoring them properly), they should be as easy for
humans to read as possible.

[...]

Quote:
Your first objection, about commenting out large blocks, is not
really valid since to do so with /* requires only two insertions,
and you can even make those on their own new lines, which requires
no interference with the existing text at all. It's actually easier
to comment out large blocks this way than with //.

Not really. If the block being commented out already has comments,
you can't just commented out out by adding another layer of /* ... */:

/* Commenting out the following two statements

statement1; /* Here's a comment */
statement2; /* Oops, statement2 isn't commented out! */

end of block being commented out */

You're likely to get a syntax error at the end (though I'm sure cases
could be constructed where you wouldn't).

Of course, the best way to "comment out" blocks of code in C is to use
"#if 0" ... "#endif". This still has the problem that it's not
obvious if you're looking at the middle of the block, but at least it
nests properly.

Quote:
To uncomment is another story, since text is line-based but /* */ is
non-local, but in practice you have either syntax highlighting, so
the editor knows this non-local information, or you follow a common
style and put a * at the beginning of each commented line, in which
case for the purposes of automation there is no difference between
/* */ and //.

Actually, there is. Adding either "// " or "* " to the beginning of
each of a range of lines is typically a single operation. In the
latter case, adding the "/*" at the top and the "*/" at the bottom is
another separate operation (unless you define a macro to do the whole
thing, which is admittedly a reasonable approach). But again, you'll
run into problems with nested comments.

One of the major reasons I like end-of-line comments is that they're
localized. It's obvious what is and is not commented out on a given
line without having to look up or down the file for a comment
delimiter.

[...]

Quote:
But of course it's a matter of taste, and I don't expect to convince
anyone.

Of course not. For example, you wisely chose not to name the
"decent editor" with which one would be commenting out code.

Indeed.

:wq

--
Keith Thompson (The_Other_Keith) kst-u@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.
Back to top
Keith Thompson
*nix forums Guru


Joined: 28 Feb 2005
Posts: 5173

PostPosted: Wed Jun 21, 2006 8:47 pm    Post subject: Re: // vs /* Reply with quote

Kenneth Brody <kenbrody@spamcop.net> writes:
[...]
Quote:
Actually, neither of those is a "nested comment". The first is a
comment with "/*" within it, and the second is a comment with "//"
in it.

Unless, that is, your compiler supports nested /*...*/ comments, in
which case the first example hasn't ended the comment.

/* This is a comment /* with a nested comment */ inside of it. */

If your compiler supports nested /* ... */ comments, it's not a
conforming C compiler.

--
Keith Thompson (The_Other_Keith) kst-u@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.
Back to top
neutron*star
*nix forums Guru


Joined: 21 Feb 2005
Posts: 2039

PostPosted: Wed Jun 21, 2006 8:57 pm    Post subject: Re: // vs /* Reply with quote

Keith Thompson said:

Quote:
Kenneth Brody <kenbrody@spamcop.net> writes:
[...]
Actually, neither of those is a "nested comment". The first is a
comment with "/*" within it, and the second is a comment with "//"
in it.

Unless, that is, your compiler supports nested /*...*/ comments, in
which case the first example hasn't ended the comment.

/* This is a comment /* with a nested comment */ inside of it. */

If your compiler supports nested /* ... */ comments, it's not a
conforming C compiler.

I disagree. Conforming compilers are allowed to provide extensions, provided
a strictly conforming program doesn't notice those extensions. Since a
strictly conforming program won't have any nested comments in it, it won't
notice that they're allowed, so there's no conformance issue.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Back to top
bill
*nix forums Guru Wannabe


Joined: 12 May 2005
Posts: 241

PostPosted: Wed Jun 21, 2006 9:03 pm    Post subject: Re: // vs /* Reply with quote

Keith Thompson wrote:
Quote:
Ryan Reich <ryan.reich@gmail.com> writes:

To uncomment is another story, since text is line-based but /* */ is
non-local, but in practice you have either syntax highlighting, so
the editor knows this non-local information, or you follow a common
style and put a * at the beginning of each commented line, in which
case for the purposes of automation there is no difference between
/* */ and //.

Actually, there is. Adding either "// " or "* " to the beginning of
each of a range of lines is typically a single operation. In the
latter case, adding the "/*" at the top and the "*/" at the bottom is
another separate operation (unless you define a macro to do the whole
thing, which is admittedly a reasonable approach). But again, you'll
run into problems with nested comments.

One of the major reasons I like end-of-line comments is that they're
localized. It's obvious what is and is not commented out on a given
line without having to look up or down the file for a comment
delimiter.


I'll agree with Keith--
uncommenting is much easier with end-of-line comments.
Consider:
# code_that_is_commented_out
# second_line_of_commented_code
## comment in the commented out block
# another_line_of_commented_code

Removing one layer of the comment symbol exposes
the 3 lines of code and leaves the original comment
as a comment.

But for some reason I have a visceral reaction to // in
C code. I suppose I'm being completely irrational, but
it just really irks me.
Back to top
Ben Pfaff
*nix forums Guru


Joined: 08 Apr 2005
Posts: 661

PostPosted: Wed Jun 21, 2006 9:06 pm    Post subject: Re: // vs /* Reply with quote

Richard Heathfield <invalid@invalid.invalid> writes:

Quote:
Since a strictly conforming program won't have any nested
comments in it, it won't notice that they're allowed, so
there's no conformance issue.

Strictly conforming programs can be broken by a nested comment
extension, e.g.

/*#include <stdio.h> /* Not needed, why was this here? */
--
A competent C programmer knows how to write C programs correctly,
a C expert knows enough to argue with Dan Pop, and a C expert
expert knows not to bother.
Back to top
neutron*star
*nix forums Guru


Joined: 21 Feb 2005
Posts: 2039

PostPosted: Wed Jun 21, 2006 9:17 pm    Post subject: Re: // vs /* Reply with quote

Bill Pursell said:

Quote:

Keith Thompson wrote:
Ryan Reich <ryan.reich@gmail.com> writes:

To uncomment is another story, since text is line-based but /* */ is
non-local, but in practice you have either syntax highlighting, so
the editor knows this non-local information, or you follow a common
style and put a * at the beginning of each commented line, in which
case for the purposes of automation there is no difference between
/* */ and //.

Actually, there is. Adding either "// " or "* " to the beginning of
each of a range of lines is typically a single operation. In the
latter case, adding the "/*" at the top and the "*/" at the bottom is
another separate operation (unless you define a macro to do the whole
thing, which is admittedly a reasonable approach). But again, you'll
run into problems with nested comments.

One of the major reasons I like end-of-line comments is that they're
localized. It's obvious what is and is not commented out on a given
line without having to look up or down the file for a comment
delimiter.


I'll agree with Keith--
uncommenting is much easier with end-of-line comments.
Consider:
# code_that_is_commented_out
# second_line_of_commented_code
## comment in the commented out block
# another_line_of_commented_code

Removing one layer of the comment symbol exposes
the 3 lines of code and leaves the original comment
as a comment.

Comments should be used to comment code, not to disable it.

If you want temporarily to disable a bunch of code, this is very easy to do
without comments:

#if 0
foo(); /* widgetify the grommets */
bar(); /* fudge the custard grinder */
#endif

And you can trivially re-enable the code by changing a single character.

Quote:
But for some reason I have a visceral reaction to // in
C code. I suppose I'm being completely irrational, but
it just really irks me.

I have three principal objections to them. Firstly, it is all too easy, on
Win32 platforms, to do this:

foo(); // widgetify the grommets, default directory is c:\temp\
bar(); // why oh WHY isn't this being called?

Secondly, I've already mentioned elsethread that switching them on also
switches off ANSI conformance in my implementation.

Thirdly, they encourage the appalling practice of "commenting out" code
instead of disabling it properly.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 2 of 4 [60 Posts] Goto page:  Previous  1, 2, 3, 4 Next
View previous topic :: View next topic
The time now is Sat Jan 10, 2009 12:26 am | All times are GMT
navigation Forum index » Programming » C
Jump to:  


The eBay Song | Credit Cards | Problem Mortgage | Loans | Mortgage
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2457s ][ Queries: 11 (0.0610s) ][ GZIP on - Debug on ]