Nested Comments in C -- A recent experience

Adrian McCarthy adrian at mti.mti.com
Tue Mar 13 03:26:18 AEST 1990


A nice compiler should warn about "/*" inside of a comment by default.  (Of
course the user should be able to deactivate this warning if he/she so
desires.)

The experience recently posted could have been caught with lint or a
coverage analyzer or even a debugger in a pretty short amount of time.

I'm not so hot on C++'s comment to end-of-line scheme because I often find
myself in situations where I want to put a comment in the middle of a line
of code.  For example:

  int table [3][3] =
    {        /*  0,  1,  2 */
      /* 0 */ { 99, 42, 37},
      /* 1 */ { 13, 12, 11},
      /* 2 */ { 55, 56, 57}
    };

or:

  (void) memcpy(/*targ*/ (void *) &x, /*src*/ (void *) &y, sizeof(y));

I too want something like nesting, so that I can comment out chunks of code
rather than #ifdef'ing.  But I agree that nested comments are not the answer.
Instead I'd like to see two types of comment delimiters (like Pascal), with
the restriction that "/*" comments can only be closed by "*/" and that
"@@" (or whatever) can only be closed by "@@".  If you use the standard
comments consistenly for regular types of commenting, and the alternate
form for commenting out code, then almost everyone should be happy.  In fact,
the alternate form could be the C++ scheme of "//" to end-of-line, which is
very appropriate for trying to comment out chunks of code.

Aid.



More information about the Comp.lang.c mailing list