C Preprocessor

Chris Miller chris at cs.hw.AC.UK
Wed Jun 4 21:06:30 AEST 1986


[
  The context is a discussion of whether implementing the C Preprocessor
  as a separate program, such as /lib/cpp on Unix, is desirable
]

In article <2600061 at ccvaxa> aglew at ccvaxa.UUCP (Andy Glew) writes:

>Unfortunately, there is no longer a clean conceptual break between
>the C pre-processor and the compiler: `sizeof' can be used in
>pre-processor constant-expressions.

The latter part of the above is untrue according to the ANSI draft
of February 1986 (Section C.8.1):

	"Additional restrictions apply to a constant expression
	 that controls conditional inclusion:  The expression
	 shall not contain a 'sizeof' operator, a cast, or an
	 enumeration constant."

The intention is clearly to enable the pre-processing to be done
without knowledge of C type semantics, nor of type implementation
details on particular target machines.

However, there is a subtle point that makes it necessary that the
preprocessor know *something* about the target machine: consider

	#if ('j' - 'i' == 1)
		char conv[] = { ... }; /* ASCII conversion table */
	#else
		char conv[] = { ... }; /* EBCDIC conversion table */
	#endif
or
	#if (-1 == ~0)
		/* 2s complement code */
	#else
		/* 1s complement or sign&magnitude code */
	#endif

The evaluation of the constant expression is clearly intended to be carried
out in terms of the "execution environment" rather than the "translation
environment".  Hence the preprocessor must know about (in this case)
the target's character set and integer arithmetic implementation.
This is not necessarily undesirable, but it does make it difficult to
provide a universal implementation of a C preprocessor.
-- 
	Chris Miller, Heriot-Watt University, Edinburgh
	...!ukc!hwcs!chris   chris at hwcs.uucp	chris at cs.hw.ac.uk



More information about the Comp.lang.c mailing list