C needs reliable comments

Rahul Dhesi dhesi%cirrusl at oliveb.ATC.olivetti.com
Tue Jan 15 06:54:24 AEST 1991


The discussion of // comments started me thinking.  Did you realize
that C is one of the few programming languages, perhaps the only one,
in which you can never be sure of correctly commenting stuff out?

(a) You can use /* and */ to comment out a block of code.  Then
anywhere in that block, if a */ occurs, the comment terminates
prematurely.

(b) You can use #if 0 and #endif to comment out a block of code.  Then
if you have an invalid token or unbalanced quotes between these, the
"commented out" block of code, the compiler may abort compilation.

(c) You can use a non-C compiler and use // comments.  But if the
compiler isn't pure non-C, your comments may become invalid if a line
ends with a blackslash.  And you still can't use // to comment out a
block of code without editing each line.

C badly needs a way of commenting out a block easily.  It needs
something like this:

#comment  /* begins in column 1; everything following is a comment */
... stuff to be commented out ...
... nac esu #tnemmoc ot peek logla sersu yppah ...
#endc    /* first occurrence of #endc in column one ends #comment block */

The #comment...#endc blocks do not nest, and are required to be in
column 1, thus avoiding all necessity for parsing tokens.  What's nice
about this scheme is that, in a pinch, you could always used sed or an
equivalent editor to strip out comments if your compiler didn't support
them:

    #! /bin/sh
    # strip out #comment comments
    TMP=/tmp/$$
    for file
    do
       sed -e '/^#comment/,/^#endc/d' < "$file" > $TMP && mv $TMP "$file"
    done

So nobody will ever be completely helpless due to lack of downward
compatibility.
--
History never         |   Rahul Dhesi <dhesi%cirrusl at oliveb.ATC.olivetti.com>
becomes obsolete.     |   UUCP:  oliveb!cirrusl!dhesi



More information about the Comp.std.c mailing list