[comp.lang.c] defining a comment?

benny@techunix.BITNET (Benny Pinkas) (09/14/88)

Can I use the Preprocessor to define a comment?
I mean something like:

#define COM /*this is a comment*/

or even

#define STARTCOM /*

Usually the text after the /* is considered as a comment and is not part
of the macro definition.
I searched a dozen of C books, and didn't find something of help.

Thanks in advance

        Benny

chris@mimsy.UUCP (Chris Torek) (09/14/88)

In article <5438@techunix.BITNET> benny@techunix.BITNET (Benny Pinkas) writes:
>Can I use the Preprocessor to define a comment?

>#define COM /*this is a comment*/

This is legal; it is merely useless.

>or even

>#define STARTCOM /*

This is rather dubious.

>Usually the text after the /* is considered as a comment and is not part
>of the macro definition.

Why should you care?  You cannot (portably) view the text (if any) emitted
by the preprocessor, which may well be an integral part of the compiler.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

henry@utzoo.uucp (Henry Spencer) (09/14/88)

In article <5438@techunix.BITNET> benny%techunix.bitnet@jade.berkeley.edu (Benny Pinkas) writes:
>Can I use the Preprocessor to define a comment?

Well, maybe.  The draft standard unambiguously says "no":  comments are
scanned and deleted before preprocessing is done.  However, many existing
implementations do things differently.  The preprocessor semantics were
never very well specified.  Given that most implementations will comply
with, or at least move towards, ANSI compliance eventually, you should
not rely on it.

>#define COM /*this is a comment*/

This ends up defining COM to be white space, which is legitimate.

>#define STARTCOM /*

This isn't going to work.
-- 
NASA is into artificial        |     Henry Spencer at U of Toronto Zoology
stupidity.  - Jerry Pournelle  | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

paulb@ttidca.TTI.COM (Paul Blumstein) (09/14/88)

In article <5438@techunix.BITNET> benny%techunix.bitnet@jade.berkeley.edu (Benny Pinkas) writes:
+Can I use the Preprocessor to define a comment?
+I mean something like:
+#define COM /*this is a comment*/
+or even
+#define STARTCOM /*
+
+Usually the text after the /* is considered as a comment and is not part
+of the macro definition.
+I searched a dozen of C books, and didn't find something of help.

Perhaps, If you told us what you want to do exactly, we can be of more
help.  Why define a comment, when it is already defined?

If you wanted a whole block commented out, one technique is:

#ifdef COMMENT
...
#endif

or

#if 0
...
#endif
=============================================================================
Paul Blumstein             | "Life is what is happening while you are
Citicorp/TTI               |  busy making other plans" -- John Lennon
Santa Monica, CA           +-------------------------------------------------
{philabs,csun,psivax}!ttidca!paulb  or  paulb@ttidca.TTI.COM
If caught, the Secretary of Citicorp will disavow all knowlege of my actions.

bill@proxftl.UUCP (T. William Wells) (09/15/88)

In article <5438@techunix.BITNET> benny%techunix.bitnet@jade.berkeley.edu (Benny Pinkas) writes:
: Can I use the Preprocessor to define a comment?

No.

---
Bill
novavax!proxftl!bill

lbr@holos0.UUCP (Len Reed) (09/15/88)

From article <13544@mimsy.UUCP>, by chris@mimsy.UUCP (Chris Torek):
> In article <5438@techunix.BITNET> benny@techunix.BITNET (Benny Pinkas) writes:
>>Can I use the Preprocessor to define a comment?
> 
>>#define STARTCOM /*
> 
> This is rather dubious.
> 
>>Usually the text after the /* is considered as a comment and is not part
>>of the macro definition.
> 
> Why should you care?  You cannot (portably) view the text (if any) emitted
> by the preprocessor, which may well be an integral part of the compiler.

You misunderstand.  On Xenix and MSC 5.x (where I've tried this) consider:

#define COMMENT /*
#define CLOSE_COMMENT */

The pre-processor considers these two lines to be functionally equivalent
to the single line:

#define COMMENT /* #define CLOSE_COMMENT */

which is no different than

#define COMMENT

This is because comment structures are parsed in the 1st pass before #defines.
Comments are not stored in the pre-processor lookup table.
Later when you say:

COMMENT
	A whole bunch of stuff
CLOSE_COMMENT

the compiler tries to compile

	A whole bunch of stuff
CLOSE_COMMENT

and messes up.
-- 
    -    Len Reed

will.summers@p6.f18.n114.z1.fidonet.org (will summers) (09/17/88)

 
Len Reed writes:
 >Can I use the Preprocessor to define a comment?
 >
 >#define STARTCOM /*
 
No. (you were talking about ANSI right?). To resolve ambiguities ANSI defines 
"translation phases" that function as-if they are separate passes over the 
source file. Comments get replaced by a single whitespace character in "pass" 
3; preprocessing directives in "pass" 4.
 
 > #define COMMENT /*
 > #define CLOSE_COMMENT */
 >
 > The pre-processor considers these two lines to be functionally equivalent
 > to the single line:
 >
 > #define COMMENT /* #define CLOSE_COMMENT */
 
Yup.
 
 > which is no different than
 > #define COMMENT
 
Which is precisely what "pass" 4 sees.
 
 > COMMENT
 >         A whole bunch of stuff
 > CLOSE_COMMENT
 
Try:
 
#define COMMENT 0
 
#if COMMENT
  A whold bunch of stuff
#endif  /* CLOSE_COMMENT */
 
 
One might be tempted to:
 
#define COMMENT       #if 0
#define CLOSE_COMMENT #endif
 
But ANSI (Jan '88 draft) says
  3.8.3.4 Rescanning and further replacement
    ...
    The resulting completely macro-replaced preprocessing token sequence is 
 not processed as a preprocessing directive even if it resembles one.
 
#if 0
may not be pleasing to your sense of aesthetics, but it is a well recognized C 
idiom.  Use it for a while and COMMENT CLOSE_COMMENT is likely to
become as alien to your eye as  #if 0  is now...
 
If you are adament:  write the code as you wish and run it add a trivial
pre-compile pass that maps COMMENT to #if 0 to the compile script.
 
    \/\/ill <tm>
 


--  
St. Joseph's Hospital/Medical Center - Usenet <=> FidoNet Gateway
Uucp: ...{gatech,ames,rutgers}!ncar!noao!asuvax!stjhmc!18.6!will.summers

bill@proxftl.UUCP (T. William Wells) (09/17/88)

In article <13544@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
: >#define STARTCOM /*
:
: This is rather dubious.

We use this construction in our code, it is always in something
like:

#define XYZZY /* comment ....
		 more comment */

We have never had any complaints about it.

I'd say that

#define STARTCOM /*

just defines STARTCOM as nothing and comments out anything
following the #define.

I'd also say that any compiler that didn't do this is broken.

---
Bill
novavax!proxftl!bill

chris@mimsy.UUCP (Chris Torek) (09/18/88)

>In article <13544@mimsy.UUCP> I wrote:
>: >#define STARTCOM /*
>:
>: This is rather dubious.

In article <779@proxftl.UUCP>, bill@proxftl.UUCP (T. William Wells) writes:
>I'd say that
>
>#define STARTCOM /*
>
>just defines STARTCOM as nothing and comments out anything
>following the #define.
>
>I'd also say that any compiler that didn't do this is broken.

Indeed, that is what it must do.  What was missing from
<5438@techunix.BITNET> that prompted my `dubious' was any surrounding
context.  I got the impression that the intent might be to create a
macro that acted like an open-comment `/*' pair, e.g.,

	#define TNEMMOC */
	#define COMMENT /*

	COMMENT
	text to be commented out
	TNEMMOC
	code

which of course does not work.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

mh@wlbr.EATON.COM (Mike Hoegeman) (09/19/88)

In article <3999@bsu-cs.UUCP> dhesi@bsu-cs.UUCP (Rahul Dhesi) writes:
 >In article <13604@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
 >>	COMMENT
 >>	text to be commented out
 >>	TNEMMOC
 >>	code
 >>which of course does not work.
 >Well...this may be just nitpicking, but:
 >     #define     COMMENT     "/*"
 >     #define     TNEMMOC     "*/"
 >     COMMENT
 >     text to be commented out
 >     TNEMMOC     /* sdrawkcab "tnemmoc" tsuj si siht */
 >     c code
 >Then use the preprocessor *twice* and you get
 >     c code
 >Which is now ready for compilation.

Maybe I'm missing something here but what the h*ll is wrong with
plain old

/********

********/

Sheesh!!

-mike

friedl@vsi.UUCP (Stephen J. Friedl) (09/19/88)

In article <5438@techunix.BITNET>, benny@techunix.BITNET (Benny Pinkas) writes:
>
> Can I use the Preprocessor to define a comment? I mean something like:
> 

I'm surprised nobody posted this.  Benny didn't ask about
a portable solution :-), so try this:

# define	STARTCOM	//**/*
# define	ENDCOM		*/**//

This is specifically disallowed by the dpANS, it is not available
everywhere, and it is definitely not something to use for any reason
other than curiosity.  This is intended as a curiosity only.

     Steve
-- 
Steve Friedl    V-Systems, Inc.  +1 714 545 6442    3B2-kind-of-guy
friedl@vsi.com     {backbones}!vsi.com!friedl    attmail!vsi!friedl
------------Nancy Reagan on conductance: "Just say mho"------------

peter@ficc.uu.net (Peter da Silva) (09/19/88)

In article <3999@bsu-cs.UUCP>, dhesi@bsu-cs.UUCP (Rahul Dhesi) writes:
> It is agreed that a Real ANSI-conforming C compiler might not supply a
> separate preprocessor pass, but who cares?  Such a C compiler would be
> an instant commercial failure.

Bzzzt!

There are quite a few compilers with built-in preprocessors that are not
commercial failures. I don't like this any better than you but like split
I&D and income tax it's nothing that we can do anything about. I'm just
glad that the compiler I use at home still has a seperate assembler pass.
-- 
Peter da Silva  `-_-'  Ferranti International Controls Corporation.
"Have you hugged  U  your wolf today?"            peter@ficc.uu.net

will.summers@p6.f18.n114.z1.fidonet.org (will summers) (09/20/88)

 > Date: 18 Sep 88 18:25:27 GMT
 > Organization: Eaton IMSD, Westlake Village, CA
In article <23345@wlbr.EATON.COM> mh@wlbr.EATON.COM (Mike Hoegeman) writes:
 >  >In article <13604@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
 >  >>     COMMENT
 >  >>     text to be commented out
 >  >>     TNEMMOC
 >  >>     code
 >  >>which of course does not work.
 > Maybe I'm missing something here but what the h*ll is wrong with
 > plain old
 > 
 > /********
 > 
 > ********/
 > 
 > Sheesh!!

It fails to "comment out" text with "/*" or "*/" tokens.  Like in 
text derived from commented code.

Otherwise in my mind  /**********************   or /*---------- is perferable.

   \/\/ill <tm>


--  
St. Joseph's Hospital/Medical Center - Usenet <=> FidoNet Gateway
Uucp: ...{gatech,ames,rutgers}!ncar!noao!asuvax!stjhmc!18.6!will.summers

friedl@vsi.UUCP (Stephen J. Friedl) (09/22/88)

In article <5438@techunix.BITNET>, benny@techunix.BITNET (Benny Pinkas) writes:
>
> Can I use the Preprocessor to define a comment? I mean something like:
> 

Then, in article <844@vsi.UUCP>, friedl@vsi.UUCP (*me*) writes:
> I'm surprised nobody posted this,
> 
> # define	STARTCOM	//**/*
> # define	ENDCOM		*/**//

I'll be durned, it doesn't work.  The first part does indeed
define STARTCOM to be /*, but I thought that once the STARTCOM
was replaced by /*, it would pass it onto the compiler unchanged
(the compiler accepts the same comment notation as cpp).
Apparently, however, it rescans this at cpp-time and the ENDCOM
is never macro-expanded.

I could have sworn I did this once, and I suspect that a maniac
with cpp could probably find a way to do this, but I couldn't.

Thanks to Don Seeley at U-of-Utah for throwing the egg on my face :-).

-- 
Steve Friedl    V-Systems, Inc.  +1 714 545 6442    3B2-kind-of-guy
friedl@vsi.com     {backbones}!vsi.com!friedl    attmail!vsi!friedl
------------Nancy Reagan on conductance: "Just say mho"------------

vopata@ksuvax1.cis.ksu.edu (Ed Vopata) (09/23/88)

In article <855@vsi.UUCP> friedl@vsi.UUCP (Stephen J. Friedl) writes:
>In article <5438@techunix.BITNET>, benny@techunix.BITNET (Benny Pinkas) writes:
>>
>> Can I use the Preprocessor to define a comment? I mean something like:
>> 
Yes!  The following in a sample program which will demonstrate the use of
STARTCOM and ENDCOM macros for defining the beginning and ending of a C
Comment.  Also included is the output of the preprocessor (cc -E).  I have 
compiled this program on a couple of C compilers (one provided with 4.3 BSD
and one provided by AT&T) and the program works.

Please note that in the "#define STAR *" there must be only 1 space between
the "STAR" and the "*" otherwise STARTCOM  may look like "/   *".

------- cut here -------
#define STAR *
#define STARTCOM /STAR
#define ENDCOM STAR/

STARTCOM
  Comment.c  -- by Edward Vopata (9/22/88)

  Put the text of the comment here.
  Multiple lines are allowed.
ENDCOM

main()
{
STARTCOM
  Some Comments within the main program
ENDCOM
}
------- cut here -------

The following is the ouput of the C preprocessor.

% cc -E comment.c
# 1 "comment.c"

/*
  Comment.c  -- by Edward Vopata (9/22/88)

  Put the text of the comment here.
  Multiple lines are allowed.
*/

main()
{
/*
  Some Comments within the main program
*/
}
----------
Name     : Edward Vopata            Dept. of Computing & Information Sciences
Internet : vopata@ksuvax1.cis.ksu.edu                 Kansas State University
Bitnet   : vopata@ksuvax1.BITNET                            Manhattan, Kansas
UUCP     : {pyramid,ucsd}!ncr-sd!ncrwic!ksuvax1!vopata         (913) 532-6350

sar@datcon.UUCP (Simon A Reap) (09/23/88)

In article <768@proxftl.UUCP> bill@proxftl.UUCP (T. William Wells) writes:
>In article <5438@techunix.BITNET> benny%techunix.bitnet@jade.berkeley.edu
							(Benny Pinkas) writes:
>: Can I use the Preprocessor to define a comment?
>
>No.
>
>---
>Bill
>novavax!proxftl!bill

Pyramid C (at least) says "Comments? Don't just say no!'

How about...
        #define ASTERISK *
        #define SCOM /ASTERISK
        #define ECOM ASTERISK/
        #define PUTCOMM(a) SCOM a ECOM

Then, as sample code.....
        start comment is SCOM
        end comment is ECOM
        comment here -> PUTCOMM(will be inside a comment)

Which produces (at least on a Pyramid, using OSx4.0, in both the att
and ucb universes, with blanks and other detritus removed for brevity)...
	start comment is /*
	end comment is */
	comment here ->  /* will be inside a comment */

I *can* see a use for this. You may just want to use the pre-processor
part of cc to produce commented output (yes, I know one should use 'm4'
for this, but 'better the devil you know...' :^).

Good luck, Benny!
-- 
Enjoy,
yerluvinunclesimon                Opinions are mine - I don't even have a cat
Reach me at sar@datcon.co.uk, or ...!mcvax!ukc!pyrltd!datcon!sar

henry@utzoo.uucp (Henry Spencer) (09/24/88)

In article <855@vsi.UUCP> friedl@vsi.UUCP (Stephen J. Friedl) writes:
>> # define	STARTCOM	//**/*
>I'll be durned, it doesn't work.  The first part does indeed
>define STARTCOM to be /*...

You can't depend on this, I'm afraid.  In fact, it is not true in ANSI C:
the /**/ comment turns into a space, not into nothing.
-- 
NASA is into artificial        |     Henry Spencer at U of Toronto Zoology
stupidity.  - Jerry Pournelle  | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

henry@utzoo.uucp (Henry Spencer) (09/24/88)

In article <678@ksuvax1.cis.ksu.edu> vopata@ksuvax1.cis.ksu.edu (Ed Vopata) writes:
>Yes!  The following in a sample program which will demonstrate the use of
>STARTCOM and ENDCOM macros for defining the beginning and ending of a C
>Comment.  Also included is the output of the preprocessor (cc -E).  I have 
>compiled this program on a couple of C compilers (one provided with 4.3 BSD
>and one provided by AT&T) and the program works.

It would probably work with any compiler using the Reiser preprocessor,
which is used by most Unix compilers (and, in particular, by essentially
all PCC-derived compilers).  It won't necessarily work with brand-X
preprocessors.

I'll say it again, just in case it didn't penetrate the first time:  there
is *no* portable way to do this.  In particular, doing it in ANSI C is
fundamentally impossible, because comment stripping precedes preprocessing.
(One of X3J11's more laudable, and simultaneously more difficult, feats
was to actually make questions like this easy to answer!)
-- 
NASA is into artificial        |     Henry Spencer at U of Toronto Zoology
stupidity.  - Jerry Pournelle  | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

will.summers@p6.f18.n114.z1.fidonet.org (will summers) (09/24/88)

In article <678@ksuvax1.cis.ksu.edu> vopata@ksuvax1.cis.ksu.edu (Ed Vopata)
writes:
 > In article <855@vsi.UUCP> friedl@vsi.UUCP (Stephen J. Friedl) writes:
 
 > The following in a sample program which will demonstrate the use of
 > STARTCOM and ENDCOM macros for defining the beginning and ending of a C
 > Comment. ...  I have
 > compiled this program on a couple of C compilers (one provided with 4.3 BSD
 > and one provided by AT&T) and the program works.
 >
 > Please note that in the "#define STAR *" there must be only 1 space between
 > the "STAR" and the "*" otherwise STARTCOM  may look like "/   *".
 >
 > ------- cut here -------
 > #define STAR *
 > #define STARTCOM /STAR
 > #define ENDCOM STAR/
 >
 > STARTCOM
 >   Comment.c  -- by Edward Vopata (9/22/88)
 >
 >   Put the text of the comment here.
 >   Multiple lines are allowed.
 > ENDCOM
 
Interesting... Will it always work under dpANS?  (I doubt it).  Even
though the code will look like
 
/*
  < comments >
*/
 
after translation phase 4, subsequent translation phases need not interpret
this as a comment: they expect comments to have been stripped. (2.1.1.2)
 
On second thought "need not" may not be strong enough:  perhaps it should
be "subsequent translation phases **may not** interpret
this as a comment".  Since STARTCOM..ENDCOM will generate code with
syntax errors, a conforming implementation -must- issue a diagnostic
message. (2.1.1.3)
 
As long as a "preprocess only" option is available, however,
running the code through -twice- would seem to work under dpANS...  But
then a simpler pre-compile pass can map STARTCOM to /* if desired.
 
dpANS aside, STARTCOM..ENDCOM seems non-portable unless the order of
macro-expansion/comment-striping is specified somewhere pre-ANSI.  It does
not seem to have been specified in K&Rv1.
 
    \/\/ill
 


--  
St. Joseph's Hospital/Medical Center - Usenet <=> FidoNet Gateway
Uucp: ...{gatech,ames,rutgers}!ncar!noao!asuvax!stjhmc!18.6!will.summers

nagel@paris.ics.uci.edu (Mark Nagel) (09/25/88)

In article <24@datcon.UUCP> sar@datcon.co.uk (Simon A Reap) writes:
|
|How about...
|        #define ASTERISK *
|        #define SCOM /ASTERISK
|        #define ECOM ASTERISK/
|        #define PUTCOMM(a) SCOM a ECOM
|
|Then, as sample code.....
|        start comment is SCOM
|        end comment is ECOM
|        comment here -> PUTCOMM(will be inside a comment)
|
|Which produces (at least on a Pyramid, using OSx4.0, in both the att
|and ucb universes, with blanks and other detritus removed for brevity)...
|	start comment is /*
|	end comment is */
|	comment here ->  /* will be inside a comment */
|
|I *can* see a use for this. You may just want to use the pre-processor
|part of cc to produce commented output (yes, I know one should use 'm4'
|for this, but 'better the devil you know...' :^).

I can see all sorts of problems from this.  Why in the world is
everyone so hot to redefine the comment tokens?  I can just see it
now:

SCOM
Add 1 to foo
foo++;
ECOM

and then later in development...

SCOM
Add 1 to foo
foo++;		/* foo controls the register bar */
ECOM

Hmm.  Why is my code blowing up?

-- 
Mark Nagel
Department of Information and Computer Science, UC Irvine
nagel@ics.uci.edu             (ARPA)             When they ship styrofoam...
{sdcsvax|ucbvax}!ucivax!nagel (UUCP)             ...what do they pack it in?

karl@haddock.ima.isc.com (Karl Heuer) (09/27/88)

No, there is no legal way in ANSI C (or in K&R C, according to K&R) to "define
a comment".  Yes, you can trick the Reiser cpp into doing it anyway.  Except
for the Obfuscated C Contest, there is little reason to want to do so.

If you're trying to suppress code which contains comments, the appropriate
notation is "#if 0" ... "#endif".  (Or, if your preprocessor is so backward
as to not have #if, "#ifdef notdef" ... "#endif" will do.)  Generally, you
should use /* ... */ for natural language commentary, and #if to remove code.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint