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

  1. /*                       if2.c
  2.  *
  3.  *   Synopsis  - A decimal integer is read as input, added to
  4.  *               CONSTANT, and the sum is displayed. If the sum
  5.  *               is less than 20, one message is displayed.
  6.  *               If not, another message is displayed.
  7.  *
  8.  *   Objective - Illustrates a simple form of the if-else
  9.  *               statement.
  10.  */
  11.  
  12. /* Include Files */
  13. #include <stdio.h>
  14.  
  15. /* Constant Definitions */
  16. #define CONSTANT 5
  17.  
  18. /* Function Prototypes */
  19. int add_const( int intvar, int constant );
  20. /*   PRECONDITION:  intvar and constant are integers
  21.  *
  22.  *   POSTCONDITION: The value returned is the sum of its parameter, 
  23.  *                  intvar and constant
  24.  */
  25.  
  26. int main( void )
  27. {
  28.      int sum, intvar;
  29.  
  30.      printf( "Enter a decimal integer: " );
  31.      scanf( "%d", &intvar );
  32.      sum = add_const( intvar, CONSTANT );
  33.      printf( "%d + %d is %d.\n", intvar, CONSTANT, sum );
  34.  
  35.      if ( sum < 20 )
  36.           printf( "The number is small enough.\n" );
  37.      else                                          /* Note 1 */
  38.           printf( "Oops, too big.\n" );
  39.      return 0;
  40. }
  41.  
  42. /******************************* add_const() *********************/
  43.  
  44. int add_const( int intvar, int constant )
  45. {
  46.      return( intvar + constant );
  47. }
  48.