E18 Answer


Answer 1: The program compiles and runs, however the compiler warns that there is a type mismatch of dee() between the implicit declaration of dee() (in dum()) and the actual definition of dee() later in the code.

Answer 2: The program runs without complaint, but it produces an incorrect result.

scope m: x=12544.000000

When the compiler first encounters dee(), it implicitly assumes that dee() returns an int, so the compiler outputs machine language appropriate for an int return value. Later on, the compiler sees that dee() returns a double, but it is too late to change what it has done. At run time, the machine language intended for an int return value gets a double return value, and this causes problems.

Some languages require compilers to make multiple passes over the source code to resolve such problems. C expects the programmer to provide prototypes so that only one pass is needed.

Answer 3: The problem can be fixed by placing a prototype for dee() ahead of its first use. Often programmers put in prototypes for all functions, even though only some are needed. This prevents unexpected problems which may be very hard to debug.


#include <stdio.h>
double dum( double x );
double dee( double a );

double dum( double x )
{
  x -= 1.0 ;
  x = dee(x);
  return x;
}

double dee( double a )
{
  if ( a > 10.0 )
    a = dum(a);
  else
    a -= 2.0;

  return a;
}

int main ( void )
{
  double x;
  
  x = dum(7.0);
  printf("scope m: x=%lf\n", x);

  system("pause") ;
  return 0 ;
}