[comp.lang.c] Something new in C syntax

allen@bunker.UUCP (C. Allen Grabert) (01/28/89)

I encountered a block of code similar to the one below, with "jjj" NOT
defined to the preprocessor, and, essentially, could not figure out why
the first "printf" did not appear.  I have since decided that the ifdef's
do NOT remove the action of the comment delimiters.  I did not write the
code, but thought, like the author, that the first "printf" should not
have been commented out.  Anyone like to comment on why it works the way
it does??


main()
{
#ifdef jjj
/*
#endif
	printf("hello world\n");
#ifdef jjj
*/
#endif
	printf("goodbye cruel world\n");
}

djones@megatest.UUCP (Dave Jones) (01/28/89)

> 
> main()
> {
> #ifdef jjj
> /*
> #endif
> 	printf("hello world\n");
> #ifdef jjj
> */
> #endif
> 	printf("goodbye cruel world\n");
> }

I'm generally one of the cooler heads when it comes to innovative
programming styles.  "Code and let code," could be my motto. I usually
don't have much sympathy for the religious moralist kind of programmer
who insists that his own style is the One True Way.

But in this particular case, if a committee voted to draw and quarter
the perpetrator, I would not be conspicuously vocal in his defense.


                     Dave Jones, Hackist


P.S. The "more included text than text" feature of inews sucks pond scum.

                     Dave J.

P.P.S. If you receive more than one copy of this, I'm godamoddy sorry.

                     D.J.

gwyn@smoke.BRL.MIL (Doug Gwyn ) (01/28/89)

In article <4586@bunker.UUCP> allen@bunker.UUCP (C. Allen Grabert) writes:
>I have since decided that the ifdef's do NOT remove the action of the
>comment delimiters.

Of course not, since comments are processed before preprocessing
directives.  This isn't "new".

chris@mimsy.UUCP (Chris Torek) (01/28/89)

In article <4586@bunker.UUCP> allen@bunker.UUCP (C. Allen Grabert) writes:
>... I have since decided that the ifdef's do NOT remove the action
>of the comment delimiters.

(correct)

>...  Anyone like to comment on why it works the way it does??

Because dmr did it that way.

More realistically: the preprocessor elides comments, and also must
scan the contents of false `#if' controls to keep track of nesting.
The most obvious way to do this is to have a lexical scanner that
deletes comments, and a parser that parses `#' control lines, and
to continue to delete comments even when parsing only to find the
matching `#endif'.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

mue@ugun21.UUCP (01/30/89)

I think the preprocessor handles comments before #'s, and so the
whole range between /* and */ is discarded.

You can check this by 'cc -E <your_program>.c '.

H.Mueller
Nixdorf Computer
D-4790 Paderborn
Germany
!USA:...mcvax!unido!nixpbe!hmueller.pad
USA: ...uunet!linus!nixbur!nixpbe!hmueller.pad

diamond@csl.sony.JUNET (Norman Diamond) (01/30/89)

In article <4586@bunker.UUCP> allen@bunker.UUCP (C. Allen Grabert) writes:
> >I have since decided that the ifdef's do NOT remove the action of the
> >comment delimiters.

In article <9522@smoke.BRL.MIL>, gwyn@smoke.BRL.MIL (Doug Gwyn ) writes:
> Of course not, since comments are processed before preprocessing
> directives.  This isn't "new".

True, it isn't new; it's only surprising and inconsistent.  In other
cases, whichever construct occurs first (in a left-to-right scan) takes
priority.  For example, "#ifdef" is a string but not a preprocessor
directive; /* "abc */ is a comment but not a syntax error (of unclosed
string), etc.  Someone intuitively guessed that /* coming first would
comment out the # that comes on the next line.  Intuition is dangerous
to C programs.
-- 
Norman Diamond, Sony Computer Science Lab (diamond%csl.sony.jp@relay.cs.net)
  The above opinions are my own.   |  Why are programmers criticized for
  If they're also your opinions,   |  re-inventing the wheel, when car
  you're infringing my copyright.  |  manufacturers are praised for it?

gwyn@smoke.BRL.MIL (Doug Gwyn ) (01/31/89)

In article <10024@diamond.csl.sony.JUNET> diamond@csl.sony.JUNET (Norman Diamond) writes:
>In other cases, whichever construct occurs first (in a left-to-right
>scan) takes priority.

That's simply not true.

scm@datlog.co.uk ( Steve Mawer ) (02/03/89)

In article <4586@bunker.UUCP> allen@bunker.UUCP (C. Allen Grabert) writes:
>                                                     I did not write the
>code, but thought, like the author, that the first "printf" should not
>have been commented out.
>
>main()
>{
>#ifdef jjj
>/*
>#endif
>	printf("hello world\n");
>#ifdef jjj
>*/
>#endif
>	printf("goodbye cruel world\n");
>}


If you've ever looked at the output from
the C compiler's preprocessor (on every
system *I've* seen, anyway) you'll find
that all comment lines are replaced with
blank lines.

(The whitespace from #included files has
to be seen to be believed :-)).

That being the case, it's the preprocessor
that handles the comments, so the lines upto,
but exluding, the last #endif, will be
blanked.  This may be confusing but it's
predictable (and expected?).




-- 
Steve C. Mawer        <scm@datlog.co.uk> or < {backbone}!ukc!datlog!scm >
                       Voice:  +44 1 863 0383 (x2153)