ANSI C -- site identification

Martin Minow minow at decvax.UUCP
Mon Dec 15 02:16:05 AEST 1986


This is one of a collection of comments on the Draft Standard, posted to
comp.lang.c for discussion before I mail a final draft to the Ansi C
committee.  Each message discusses one problem I have found with the Draft
Standard that I feel warrants a "no" vote.  Note that this message is my
personal opinion, and does not reflect on the opinions of my employer.

---- Problem:

Page 82, line 34. Many, if not all, existing implementations pre-define
implementation-specific preprocessing variables that specify the processor,
operating system, and, in some cases, the compiler name.  For example, Decus
C predefines ``pdp11'', ``decusc'', and either ``rt11'' or ``rsx.''  For
better or for worse, this tradition is almost ten years old.  Some provision
should be made for this in Standard C.

---- Motivation:

Page 82, line 34.  A provision is needed to permit implementation-specific
preprocessor definitions.  The existing practice of predefining a
more-or-less random collection of variables does not work well, but the
capability is essential to anyone writing portable programs.

The following is presented as a possible solution, but I would note
that it has never been implemented:

  -- Extend the syntax of the #if statement expression to permit comparing
     two strings.  The relational and equality operators would use the
     strcmp() function when both operands are quoted strings.

  -- Add (at least) the following predefined variables to the preprocessor
     (the values are implementation dependent character strings, and are
     defined as \verb+""+ if unspecified).  These variables may be
     un- and re-defined by the program.

     __PROCESSOR__	defines the processor that the compiler is
			targetted for.

     __SYSTEM__		defines the operating system that the compiler is
			targetted for.

     __COMPILER__	defines the compiler family name (this could be a
			manufacturer's name).

     __VERSION__	defines the compiler release or patch level.

     __HOST_SYSTEM__	defines the operating system on which the compiler
			is running (this is needed to specify #include
			file names).

When this is done, the programmer could write conditional expressions
such as

    #if __PROCESSOR__ == "pdp11" && __VERSION__ > "V4.01"
    ...
    #endif

I have done essentially the above in a number of programs, such as Decus cpp,
that operate on a variety of processor and operating system configurations.

The changes that would need to be made to the \verb+#if+ processor
are roughly as follows:

  -- The lexical analyser must accept string constants.

  -- The expression evaluator must test for the case where both
     operands are strings and a relational or equality operator
     is being evaluated.  If this is the case, strcmp() is called
     and the evaluation stack changed to appropriate numeric values
     (<, >, <=, >=, and == are straighforward; != requires some fudging).
     The evaluation then proceeds normally.

  -- The # and ## operators must be added, with appropriate precedence.
     Then, one could write

	#define RELEASE	1
	#define PATCH	3
	#if __VERSION__ >= #RELEASE ## "." ## #PATCH


----

Martin Minow
decvax!minow



More information about the Comp.lang.c mailing list