Sams Teach Yourself C in 21 Days

(singke) #1
void print_msg(void)
{
puts( “This is a message to print” );
}


  1. There should not be a semicolon at the end of the function header.

  2. Only the larger_of()function needs to be changed:
    21: int larger_of( int a, int b)
    22: {
    23: int save;
    24:
    25: if (a > b)
    26: save = a;
    27: else
    28: save = b;
    29:
    30: return save;
    31: }

  3. The following assumes that the two values are integers and an integer is returned:
    int product( int x, int y )
    {
    return (x * y);
    }

  4. The following listing checks the second value passed to verify that it is not 0.
    Division by zero causes an error. You should never assume that the values passed
    are correct.
    int divide_em( int a, int b )
    {
    int answer = 0;
    if( b == 0 )
    answer = 0;
    else
    answer = a/b;
    return answer;
    }

  5. Although the following code uses main(), it could use any function. Lines 9, 10,
    and 11 show the calls to the two functions. Lines 13 through 16 print the values. To
    run this listing, you need to include the code from exercises 7 and 8 after line 19.
    1: #include <stdio.h>
    2:
    3: int main( void )
    4: {


838 Appendix F

49 448201x-APP F 8/13/02 11:22 AM Page 838

Free download pdf