Clever C Preprocessor Definitions Sought

John Hascall hascall at atanasoff.cs.iastate.edu
Sat Sep 2 01:38:59 AEST 1989


In article <555 at stdc01.UUCP> mjones at stdc01.UUCP (Michael Jones) writes:
}I am interested in collecting popular pre-processor definitions.
 
   Probably everyone has re-invented some variation on:

      #define  ALLOC(type)  ((type *) malloc(sizeof(type)))

   As well as various constants:

      #define K_PI             3.141592653589793238462643
      #define K_E              2.718281828459045235360287

      #define K_PI_2           1.570796326794896619231322
      #define K_PI_4           0.785398163397448309615661
      #define K_PI_8           0.39269908169872415480783
      #define K_PI_180         0.0174532925199432957692369
      #define K_1_PI           0.3183098861837906715377675
      #define K_2PI            6.283185307179586476925287

      #define K_LN_2           0.693147180559945309417232
      #define K_LN_3           1.098912288668109691395245
      #define K_LN_10          2.302585092994045684017991

      #define K_180_PI        57.295779513082320876798155
      #define K_PI_180         0.017453292519943295769237
      #define K_ARC_MINUTE     0.000290888208665721596154
      #define K_ARC_SECOND     0.000004848136811095359936

   Here is one I use on VMS where system calls return odd values for OK
   and even values for various errors (but which has a couple of ideas
   which could be applied elsewhere):

      #define ODD(x)             ((x)&1)
      #define EVEN(x)            (!ODD(x))
      #define ERROR(cond)        EVEN(cond)
      #define FATAL(str, val)    exit((fprintf(stderr, "[%4d] 0x0%x <- %s\n", \
				       __LINE__, val, str), val))
      #define SYSCALL(v, func)   if (ERROR(v = func)) FATAL("func", v)

      Used like this for example:

         register int cond;
         SYSCALL(cond, SYS$ASSIGN(&device, &channel, 0, 0));

         which gives error messages like:

	     [  17] 0x0908 <- SYS$ASSIGN(&device, &channel, 0, 0)
	     No such device available

         (assuming the SYSCALL is on line 17)



More information about the Comp.lang.c mailing list