home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0000 - 0009 / ibm0000-0009 / ibm0003.tar / ibm0003 / LCNOW2.ZIP / EXAMPLES / ADD_ANY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-07-10  |  716 b   |  38 lines

  1. /*
  2.  * A D D _ A N Y
  3.  *
  4.  * Add values without regard to data type.  This program uses
  5.  * a macro to do the calculation instead of using a separate
  6.  * function for each data type.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. #define ADD(a, b) ((a) + (b))
  13.  
  14. int
  15. main()
  16. {
  17.     int n1 = 20, n2 = 25;
  18.     float f1 = 12.34, f2 = 56.78;
  19.  
  20.     /*
  21.      * Use the ADD macro to add a pair of integers.
  22.      */
  23.     printf("%d + %d = %d\n", n1, n2, ADD(n1, n2));
  24.  
  25.     /*
  26.      * Use the ADD macro to add a pair of reals.
  27.      */
  28.     printf("%f + %f = %f\n", f1, f2, ADD(f1, f2));
  29.  
  30.     /*
  31.      * We can even use the ADD macro with
  32.      * operands of mixed data types.
  33.      */
  34.     printf("%d + %f = %f\n", n1, f1, ADD(n1, f1));
  35.  
  36.     return (0);
  37. }
  38.