[comp.lang.c] C, embedded comments and preprocessors

lwv27@CAS.BITNET (09/20/90)

I have not been able to afford a copy of the new ANSI std for c,
and perhaps it clarifies the situation.

I have for some time commented out portions of code by surrounding them by
a construct similar to:
/*
lines of text

lines of code   /*  line comment */

lines of various text's comments

lines of code   /*  line comment */
*/

Today, I found a case where a third party vendor's preprocessor turned
off its idea that it was in a comment.  It did not however complain about
it until it ran into a line like:

lines of various text's comments

at which time it complained that I had started but didnt finish up
a character constant.

Is it common for a preprocessor to disallow embedded comments?

I am not looking for a debate over style, better techniques of how to
do this, etc.  I am just trying to decide if this is a unique
occurance, fairly common, a required behavior by ANSI C stds now, etc.

I do not want to contribute to flame wars on technologically religious
issues ;-[)

--
Larry W. Virden
Business: UUCP: osu-cis!chemabs!lwv27  INET: lwv27%cas.BITNET@CUNYVM.CUNY.Edu
Personal: 674 Falls Place,   Reynoldsburg,OH 43068-1614
Proline: lvirden@pro-tcc.cts.com  America Online: lvirden CIS: [75046,606]

mills@ccu.umanitoba.ca (Gary Mills) (09/20/90)

lwv27@CAS.BITNET writes:

>I have not been able to afford a copy of the new ANSI std for c,
>and perhaps it clarifies the situation.

You are relying on nested comments, and even the original K&R states
that `comments do not nest'.

>I have for some time commented out portions of code by surrounding them by
>a construct similar to:

A better way to remove some code from the compilation is with:

#ifndef OLD_STUFF

lines of code   /*  line comment */

lines of various text's comments

lines of code   /*  line comment */

#endif

-- 
-Gary Mills-             -University of Manitoba-             -Winnipeg-

karl@haddock.ima.isc.com (Karl Heuer) (09/20/90)

In article <1990Sep20.000536.19987@ccu.umanitoba.ca> mills@ccu.umanitoba.ca (Gary Mills) writes:
>lwv27@CAS.BITNET writes:
>>I have for some time commented out portions of code by surrounding them by
>>a construct similar to: [attempt to use /* ... /* ... */ ... */]

No offense, but I'm astounded that some people think nested comment-parsing
is normal behavior.  (Whether it *should* be is irrelevant.  The fact is that
it isn't and never has been%.)

>A better way to remove some code from the compilation is with:
>	#ifndef OLD_STUFF
>	lines of code   /*  line comment */
>	lines of various text's comments
>	lines of code   /*  line comment */
>	#endif

No, that still won't work$.  The middle line of those three was commentary
rather than code, and the apostrophe is still an unclosed character constant.
The correct solution is to use /*...*/ for comments and #if...#endif for code
removal, e.g.
	#if 0
	lines of code   /*  line comment */
	/*
	lines of various text's comments
	*/
	lines of code   /*  line comment */
	#endif

Karl W. Z. Heuer (karl@kelp.ima.isc.com or ima!kelp!karl), The Walking Lint
________
% Don't bother telling me it works in *your* compiler.  I don't care; it still
  isn't legal.
$ See previous footnote.

henry@zoo.toronto.edu (Henry Spencer) (09/20/90)

In article <9009191929.AA13849@lilac.berkeley.edu> lwv27@CAS.BITNET writes:
>...until it ran into a line like:
>
>lines of various text's comments
>
> ...
>Is it common for a preprocessor to disallow embedded comments?
>... I am just trying to decide if this is a unique
>occurance, fairly common, a required behavior by ANSI C stds now, etc.

ANSI C is quite explicit that everything outside comments (remembering
that comments do not nest in C, and never have) must be legal tokens,
or at least preprocessor tokens if inside the body of a "false" #if[][def]
(which is what you should be using to delete text).  There is some weasel-
wording which permits a compiler to be tolerant of lines like yours, *if*
they are inside a false #if, but this is not required behavior and relying
on it is not portable.

You should be using #if to "comment out" program text, and putting human-
language comments inside /* */.
-- 
TCP/IP: handling tomorrow's loads today| Henry Spencer at U of Toronto Zoology
OSI: handling yesterday's loads someday|  henry@zoo.toronto.edu   utzoo!henry

brianh@hpcvia.CV.HP.COM (brian_helterline) (09/21/90)

mills@ccu.umanitoba.ca (Gary Mills) writes:
>lwv27@CAS.BITNET writes:

>>I have not been able to afford a copy of the new ANSI std for c,
>>and perhaps it clarifies the situation.

>You are relying on nested comments, and even the original K&R states
>that `comments do not nest'.

>>I have for some time commented out portions of code by surrounding them by
>>a construct similar to:

>A better way to remove some code from the compilation is with:

>#ifndef OLD_STUFF

>lines of code   /*  line comment */

>lines of various text's comments

>lines of code   /*  line comment */

>#endif


Even better is to use

#if 0

lines of code   /*  line comment */

lines of various text's comments

lines of code   /*  line comment */

#endif