ANSI C and the C Pre-Processor

hansen at pegasus.UUCP hansen at pegasus.UUCP
Fri Sep 7 05:25:53 AEST 1984


One of the minor(?) changes being proposed for the C standard is a change in
the C Pre-Processor to change it from a totally character oriented processor
closer to a token processor. It already does this to a certain degree by
recognizing comments as separate tokens that don't get scanned for text to
be replaced. However, this idea is being extended to include strings and
character constants as tokens that don't get scanned for replacement text.
The idea is to prevent bugs similar to the following:

#define foo(d,g)	printf("%d,%d", d, g)

This would expand 

	foo(f,e);

to

	printf("%f,%f", f, e);

and suddenly your integer variables f and e would be getting printed out in
%f format rather than %d format. Under the new rules, the expansion would be:

	printf("%d,%d", f, e);

This certainly solves the above problem. However, I have seen plenty of
programs which use some of the following constructs:

#define libpath(x)		"/usr/lib/x"
#define CTRL(x)			('x'&037)
#define PRINT1(format,arg)	printf("arg=%format.\n", arg);

A common place to find the libpath construct is in uparm.h used by (among
many others) the vi, curses, terminfo and termcap packages on both System Vr2
machines and 32V/BSD machines.

I don't know of any system code that depends on the CTRL example, but I know
of a number of people who have used it in the past.

Those of you who have read the C Puzzle Book will realize that NONE of Alan
Feuer's programs will work anymore!


The questions are: Should this change be endorsed? If so, what should be
done to bring back the lost functionality? If not, how would you make CPP
more regular in its scanning rather than that which is the de-facto standard
from Reiser?

One possibility would be to introduce a new construct #sdefine which has the
special property that strings and character constants would also be scanned
for replacement; otherwise it would be identical to #define. The programs
which use the above constructs would have to be changed to use #sdefine
instead of #define, but no other changes would have to be made.

Without something to replace the lost functionality, I feel that the number
of programs which would have to be changed would be major.

					Tony Hansen
					pegasus!hansen



More information about the Comp.lang.c mailing list