/* Programmer: Greg Perkins Program: macro_min.c Class: CS 73 C/UNIX Due: March 12, 1998 This program uses user-defined macros to determine the minimum value between two values. It also uses the two value definition as the base of a three value definition and subsequently uses the three value definition as the base of a four value definition. In the event of two equal values, the program actually gives the value of the second number, this is arbitrary since the two values are equal anyway. Changing the value of 'X' in the statement '#define TYPE X' will change the type of variable being tested in this example program. */ #include #define MIN(a, b) ((a) > (b) ? (b) : (a)) #define MIN_3(a, b, c) ((a) > (b) ? (MIN(b, c)) : (MIN(a, c))) #define MIN_4(a, b, c, d) ((a) > (b) ? (MIN_3(b, c, d)) : (MIN_3(a, c, d))) /* TYPE controls the comparison variable type TYPE == 1, Integer Definitions used TYPE == 2, Float Definitions used TYPE == ?, Char Definitions used {default} */ #define TYPE 2 #if TYPE == 1 /*Integer Definitions*/ #define LITERAL %d #define STRING1 "Variables used are integers:\n" #define STRING2 "%d : %d : %d : %d \n\n" #define STRING3 "Call %d : %d\n" #elif TYPE == 2 /*Float Definitions*/ #define LITERAL %f #define STRING1 "Variables used are floats:\n" #define STRING2 "%f : %f : %f : %f \n\n" #define STRING3 "Call %d : %f\n" #else /*Char Definitions*/ #define LITERAL %c #define STRING1 "Variables used are chars:\n" #define STRING2 "%c : %c : %c : %c \n\n" #define STRING3 "Call %d : %c\n" #endif int main() { int call = 0; #if TYPE == 1 int one = 1, two = 2, three = 3, four = 4; #elif TYPE == 2 float one = 2.1, two = 2.2, three = 2.3, four = 2.4; #else char one = 'a', two = 'b', three = 'c', four = 'd'; #endif printf(STRING1); printf(STRING2, one, two, three, four); printf(STRING3, ++call, MIN(one, two)); printf(STRING3, ++call, MIN(two, one)); printf(STRING3, ++call, MIN(one, one)); printf(STRING3, ++call, MIN_3(one, two, three)); printf(STRING3, ++call, MIN_3(one, three, two)); printf(STRING3, ++call, MIN_3(two, one, three)); printf(STRING3, ++call, MIN_3(two, three, one)); printf(STRING3, ++call, MIN_3(three, one, two)); printf(STRING3, ++call, MIN_3(three, two, one)); printf(STRING3, ++call, MIN_3(one, one, two)); printf(STRING3, ++call, MIN_3(one, two, one)); printf(STRING3, ++call, MIN_3(two, one, one)); printf(STRING3, ++call, MIN_3(one, one, one)); printf(STRING3, ++call, MIN_4(one, two, three, four)); printf(STRING3, ++call, MIN_4(one, two, four, three)); printf(STRING3, ++call, MIN_4(one, three, two, four)); printf(STRING3, ++call, MIN_4(one, three, four, two)); printf(STRING3, ++call, MIN_4(one, four, two, three)); printf(STRING3, ++call, MIN_4(one, four, three, two)); printf(STRING3, ++call, MIN_4(two, one, three, four)); printf(STRING3, ++call, MIN_4(two, one, four, three)); printf(STRING3, ++call, MIN_4(two, three, one, four)); printf(STRING3, ++call, MIN_4(two, three, four, one)); printf(STRING3, ++call, MIN_4(two, four, one, three)); printf(STRING3, ++call, MIN_4(two, four, three, one)); printf(STRING3, ++call, MIN_4(three, one, two, four)); printf(STRING3, ++call, MIN_4(three, one, four, two)); printf(STRING3, ++call, MIN_4(three, two, one, four)); printf(STRING3, ++call, MIN_4(three, two, four, one)); printf(STRING3, ++call, MIN_4(three, four, one, two)); printf(STRING3, ++call, MIN_4(three, four, two, one)); printf(STRING3, ++call, MIN_4(four, one, two, three)); printf(STRING3, ++call, MIN_4(four, one, three, two)); printf(STRING3, ++call, MIN_4(four, two, one, three)); printf(STRING3, ++call, MIN_4(four, two, three, one)); printf(STRING3, ++call, MIN_4(four, three, one, two)); printf(STRING3, ++call, MIN_4(four, three, two, one)); printf(STRING3, ++call, MIN_4(one, one, two, three)); printf(STRING3, ++call, MIN_4(one, two, one, three)); printf(STRING3, ++call, MIN_4(one, two, three, one)); printf(STRING3, ++call, MIN_4(two, one, one, three)); printf(STRING3, ++call, MIN_4(two, one, three, one)); printf(STRING3, ++call, MIN_4(two, three, one, one)); printf(STRING3, ++call, MIN_4(one, one, one, two)); printf(STRING3, ++call, MIN_4(one, one, two, one)); printf(STRING3, ++call, MIN_4(one, two, one, one)); printf(STRING3, ++call, MIN_4(two, one, one, one)); printf(STRING3, ++call, MIN_4(one, one, one, one)); return (0); }