Clever C Preprocessor Definitions Sought

Michael Jones mjones at stdc01.UUCP
Thu Aug 31 00:57:32 AEST 1989


I am interested in collecting popular pre-processor definitions. The
"semi-extensibility" which C offers via CPP provides an example set
for those curious about common usage, missing language features, etc.

Some ideas that I have seen or which seem useful to me would include:

    1. Each of the high-precision numerical constants from the
       back of Knuth's Art of Computer Programming, in appropriate
       float/double real precision. In fact, since some compilers
       fail slightly on floating input conversion, it might be
       nice to see these numbers as "*(float *)" and "*(double *)"
       indirections to explicit binary values stored as character
       arrays of proper length, where "proper" rounding had been 
       used to construct them.

       Definitions for ANSI Terminal control sequences and ASCII
       control characters would also be useful. I use something like
       this when writing for the IBM-PC to decode "keyboard scan-
       codes":

       #define SCAN_left_shift_key	0xNN	(don't remember now)
       #define SCAN_keypad_plus		0xNN

    2. Variable and or type definitions. I have found explicit size
       definitions to be handy:

       #define ordinal_8	unsigned char
       #define integer_8	char
	 :
	 :
       #define ordinal_32	unsigned long
       #define integer_32	long

    3. Syntactic type definitions, ranging from the simple/suave to
       the "ambitious". I have seen several versions of "improved"
       selection statements, unbounded loops, sequential iterators,
       and so on. Examples of this nature include:

       a. IF/THEN/ELSE constructs:

       #define IF       if (
       #define THEN     ) {
       #define ELSE     } else {
       #define ELSEIF   } else if (
       #define ENDIF    }

       IF a == b THEN
           x = 1;
           b = 2;
       ELSE
           y = 3;
       ENDIF

       (If this example shocks you, arrange to view the S.R. Bourne "SH" 
       source code! Almost anyone would think it was in algol-60.)

       b. SELECTION constructs:

       #define when         break; case
       #define also         case
       #define otherwise    break; default

       switch (expression)
           {
	   when value-1:
	   also value-2:
	       code;
	   when value-3:
	       code;
	   otherwise:
	       code;
	   }


       c. UNBOUNDED ITERATION constructs:

       #define ever	1

       for (ever)
	   code;

       #define repeat	for (;;)

       repeat
	   code;

    4. Macro "pseudo-functions" from the simple and common min/max/abs:

       #define min(a,b)	((a) > (b) ? (b) : (a))

       to the more extreme "quick-sort generator given base data type"
       which I have seen recently.

If you have anything like this which you would like to contribute to the
"C-Preprocessor Library", I would like to hear from you! Just think, that
handy little definition could be remembered for posterity as:

    "... well son, back when *I* was a programmer, we used something
    known as Galt's Device, a really nifty CPP definition that came
    across the net. Of course, back then we had to ..."

where for Galt read *your name here*. I promise to share the full results
with everyone who submits something interesting. Or will post if there is
sufficient activity.

-- 
-- Michael T. Jones          Email:            ...!mcnc!rti!stdc01!mjones --
-- The wise man will pursue  Paper: 3101-H Aileen Drive, Raleigh NC 27606 --
-- excellence in all things  Voice: W:(919)361-3800  and  H:(919)851-7979 --



More information about the Comp.lang.c mailing list