[comp.lang.c] Directives in comments

compata@cup.portal.com (David H Close) (12/21/89)

Any computer language is really defined by its implementations, not by
the formal specifications.  If most or every implementation consistently
violates the specification in some specific way, the de facto specification
is changed to conform to the implementation.  I have recently encountered
an instance of C syntax which is unacceptable to my compiler but which
appears should be correct by the specifications.  I'd appreciate any comments
from the net as to whether this represents a compiler bug or a de facto
change to the specifications.

In the SCO Xenix C Language Reference (SCO-514-210-014, 6-21-87), page 8-1,
it states:  "Directives can appear anywhere in a source file, but they apply
only to the remainder of the source file in which they appear."  This appears
to be consistent with K&R (1978), which states on page 207, "These lines
[beginning with a #] have syntax independent of the rest of the language;
they may appear anywhere and have effect which lasts (independent of scope)
until the end of the source program file."

The following program fragment appears to conform to the rules stated.

	/*
	 *            Program heading
	 #define hdr "Program name"
	 *            end of heading
	 */
	main () { printf(hdr); }

However, the SCO compiler produces the following error message.

	t.c(6) : error 65: 'hdr' : undefined

K&R's assertion that directives "have syntax independent of the rest of
the language" would appear to me to allow the example.  However, this
appears to me to be so fundamental to C that, if it were truly a bug,
it couldn't have survived until now.

If you post a reply, please email me a copy to insure I don't miss it.

Dave Close, Compata, Arlington, Texas
compata@cup.portal.com

reino@cs.eur.nl (Reino de Boer) (12/22/89)

compata@cup.portal.com (David H Close) writes:

>[...text deleted...] This appears
>to be consistent with K&R (1978), which states on page 207, "These lines
>[beginning with a #] have syntax independent of the rest of the language;
>they may appear anywhere and have effect which lasts (independent of scope)
>until the end of the source program file."

>The following program fragment appears to conform to the rules stated.

>	/*
>	 *            Program heading
>	 #define hdr "Program name"
>	 *            end of heading
>	 */
>	main () { printf(hdr); }

>K&R's assertion that directives "have syntax independent of the rest of
>the language" would appear to me to allow the example.

In K&R Second Edition pp. 228--229, A12. Preprocessing:
	3. The program is split into tokens separated by white-space
	characters; comments are replaced by a single space. Then
				^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^(only then)
	preprocessing directives are obeyed, and macros (...) are expanded.

Hope this explains -- Reino

-- 
Reino R. A. de Boer
Erasmus University Rotterdam ( Informatica )
e-mail: reino@cs.eur.nl

exspes@gdr.bath.ac.uk (P E Smee) (12/22/89)

In article <25234@cup.portal.com> compata@cup.portal.com (David H Close) writes:

>The following program fragment appears to conform to the rules stated.
>
>	/*
>	 *            Program heading
>	 #define hdr "Program name"
>	 *            end of heading
>	 */
>	main () { printf(hdr); }
>
>However, the SCO compiler produces the following error message.
>
>	t.c(6) : error 65: 'hdr' : undefined

Common folklore has it that comments are stripped out (and always have
been) BEFORE the preprocessor sets to work, so that your #define
disappears before the preprocessor sees it.  However, I can't find
anything in either K&R-1 or K&R-2 which specifically SAYS that.  The
behaviour you mention certainly doesn't surprise me, though.

-- 
Paul Smee, Univ of Bristol Comp Centre, Bristol BS8 1TW, Tel +44 272 303132
 Smee@bristol.ac.uk  :-)  (..!uunet!ukc!gdr.bath.ac.uk!exspes if you MUST)

davis@hplabsz.HPL.HP.COM (Jim Davis) (12/29/89)

David H Close <compata@cup.portal.com> wrote:
dhc>	[...text deleted...] This appears to be consistent with K&R (1978), ...
dhc>	The following program fragment appears to conform to the rules stated.
dhc>		/*
dhc>		 *            Program heading
dhc>		 #define hdr "Program name"
dhc>		 *            end of heading
dhc>		 */
dhc>		main () { printf(hdr); }

dhc>	K&R's assertion that directives "have syntax independent of the
dhc>	rest of the language" would appear to me to allow the example.

Reino de Boer <reino@cs.eur.nl> quite correctly replied:
rdb>	In K&R Second Edition pp. 228--229, A12. Preprocessing:
rdb>	 3. The program is split into tokens separated by white-space
rdb>	 characters; comments are replaced by a single space. Then
rdb>	   	     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^(only then)
rdb>	 preprocessing directives are obeyed, and macros (...) are expanded.

rdb>	Hope this explains -- Reino

I would add one more nit.  The example presented does not have
"lines beginning with a #".  The "#define" was indented.  The
'#' symbol must not (at least in the compilers I am familliar with)
be preceeded by whitespace.
-- 
Jim Davis (James W. Davis)      Palo Alto, CA        (415)857-4036
davis@hplabs.hp.com             {any_of_the_biggies}!hplabs!davis