Nested Comments in C -- A recent experience

Glenn A. Emelko crds at pyrite.som.cwru.edu
Sun Mar 11 16:57:12 AEST 1990


Well, for once in a long while I've got a few cents to throw in.  Just got done
spending 3 hours looking for a "bug" which turned out not to be a "bug" at all;
rather it went undetected as a programming error BECAUSE our C compiler doesn't
handle nested comments (like most C compilers don't).  The code looked like the
example below (not the actual code, but similar):

      function(arg1,arg2) {
        int a,b,c;
        a=arg1*arg2+3;     /* Oops, we're going to forget to close this comment
        b=arg1+2*arg2;     /* Guess what, this line never gets executed */
        c=a+b;
        return(c);         /* Here's the bogus results, Mr. caller */
      }

Unfortunately, we spent very little time suspecting that "function" didn't work
properly, because it was so simple!  The compiler didn't complain about it, and
we had no reason to suspect that it wasn't returning the proper value - in fact
when we looked at what it returned we STILL were baffled - so much in fact that
we decided to look at the .asm file to find the nasty stack corruption that had
to be causing our problem (or so we thought)... and that's where I noticed that
a whole line of code was missing in the .asm code; I did a double-take and then
looked REAL carefully at the C code and spotted the missing end-of-comment.  Up
until now I had no opinion on this matter; however things have changed.  If the
C compiler understood nested comments it would FORCE programmers to match them,
preventing wasted hours of debugging on annoying problems like the above at the
expense of a few milliseconds of additional compile time.  Anyone else?

Glenn Emelko




More information about the Comp.lang.c mailing list