This one bit me today

Richard O'Keefe ok at cs.mu.oz.au
Tue Oct 24 19:20:32 AEST 1989


In article <41019 at bu-cs.BU.EDU>, austin at bucsf.bu.edu (Austin Ziegler) writes:
>    I read the rest of your article, but this is the one part I have
> problems with.  I am originally a Pascal programmer, and have been
> programming in C for enough time to acquaint myself with the language.  In
> Pascal, one of the nicest features for debugging is nested comments.

Er, there's something your best friend should have told you:
Pascal comments DO NOT NEST.  Neither in the ANSI standard, nor the ISO
standard, nor Jensen & Wirth, nor either of the Pascal compilers I just
checked (although one of them prints a warning if it sees '(*' inside '(*').

Here is a Pascal program.

      | program nestcom(input, output);
      |     begin
      |         (* comments in Pascal do not nest:
      |             (* this is not a nested comment *)
      |         writeln('This is not commented out');
      |             (* this is not a nested comment *)
      |         writeln('This *) does not close any comment');
      |     end (* nestcom *).

Compiling and running this program produces the output

      | This is not commented out
      | This *) does not close any comment

A Pascal processor which conforms to the standard may still have trouble
with it (the output lines might be too long or it might not support lower
case or something), but modulo case this is the only valid output from it.
Any Pascal processor which treats these comments as nested VIOLATES THE
STANDARD.

> I *have* found the need to
> comment it out because I am not fully acquainted with all of the features
> of the preprocessor commands.

The only preprocessor directives you need to know are

	#if 0
		<this will be tokenised but not otherwise processed>
	#endif

It is extremely important to realise that nested comments are "pushed" as
a debugging tool, BUT THEY DO NOT WORK RELIABLY FOR THAT PURPOSE.  If the
code you are commenting out contains comment delimiters, putting nested
comment delimiters around it is going to land you in big trouble.  A UNIX
C programmer might be likely to call f = popen("ls SCCS/*.p", "r");
                                                       ^^
If you have an Emacs-like editor, it is extremely easy to write a
comment-out-region command which works just fine without needing nested
comments.  Mine turns /<N spaces>* into /<N+1 spaces>* and similarly for
*<N spaces>/, comment-in-region decrements the number of spaces and
removes the outer delimiter pair.

> 		 I *AM* a Pascal guru, when I want to be.

Pascal gurus have memorized the Standard.



More information about the Comp.lang.c mailing list