home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch11 / squares.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  2KB  |  67 lines

  1. /*               squares.c
  2.  *
  3.  *   Synopsis  - Displays results of the macros SQUARE(x),
  4.  *               SQR(x), SQR2(x) and the function square(x).
  5.  *
  6.  *   Objective - To illustrate some of the differences between
  7.  *               macros and functions: how substitutions take
  8.  *               place and how macros are expanded.
  9.  */
  10.  
  11. /* Include Files */
  12. #include <stdio.h>
  13.  
  14. /* Macro Definitions */
  15. #define SQUARE( x )         x * x                       /* Note 1 */
  16. #define SQR( x )            (( x ) * ( x ))             /* Note 2 */
  17.  
  18. int SQR2_x;                                             /* Note 3 */
  19. #define SQR2( x )           ( SQR2_x=( x ), SQR2_x * SQR2_x )
  20.  
  21. /* Function Prototypes */
  22. int square( int x );
  23. /*   PRECONDITION:  x is an int
  24.  *
  25.  *   POSTCONDITION: returns the square of its argument
  26.  */
  27.  
  28. int main( void )
  29. {
  30.      int a;
  31.  
  32.      a = 3;                                             /* Note 4 */
  33.      printf( "square( a ) is %d.\n", square( a ));
  34.      printf( "SQUARE( a ) is %d.\n", SQUARE( a ));
  35.      printf( "SQR( a ) is %d.\n", SQR( a ));
  36.      printf( "SQR2( a ) is %d.\n", SQR2( a ));
  37.                                                         /* Note 5 */
  38.      printf( "\nsquare( a+1 ) is %d\n", square( a+1 ));
  39.      printf( "SQUARE( a+1 ) is %d.\n", SQUARE( a+1 ));
  40.      printf( "SQR( a+1 ) is %d.\n", SQR( a+1 ));
  41.      printf( "SQR2( a+1 ) is %d.\n", SQR2( a+1 ));
  42.  
  43.                                                         /* Note 6 */
  44.      printf( "\nsquare( a++ ) is %d, and ", square( a++ ));
  45.      printf( "a is %d\n", a );
  46.  
  47.      a = 3;
  48.      printf( "SQUARE( a++ ) is %d, and ", SQUARE( a++ ));
  49.      printf( "a is %d\n", a );
  50.  
  51.      a = 3;
  52.      printf( "SQR2( a++ ) is %d, and ", SQR2( a++ ));
  53.      printf( "a is %d\n", a );
  54.  
  55.      a = 3;
  56.      printf( "SQR( a++ ) is %d, and ", SQR( a++ ));    /* Note 7 */
  57.      printf( "a is %d\n", a );
  58.      return 0;
  59. }
  60.  
  61. /*******************************square()************************/
  62.  
  63. int square( int x )                                   /* Note 8 */
  64. {
  65.      return ( x * x );
  66. }
  67.