What happens when a function prototype is missing?
Here's a program that is missing a function prototype. It
doesn't compile. Let's look at the error messages that are produced.
Many C compilers make assumptions about functions that are
called before they are defined or declared via a function prototype
(e.g., that they always return an int ).
The Program That Doesn't Compile
/*********************************************
** File: proto.c
** Author: S. Bogar
** Date: 8/3/99
** Section: 101
** SSN: 123-45-6789
** EMail: bogar@cs.umbc.edu
**
** Demonstrate the need for prototypes
** This is the buggy version.
*********************************************/
#include
/* No prototypes in this version */
int main()
{
double x;
int n, m;
x = Add3 (5.0);
printf("x = %f\n", x);
n = Add3 (10.0);
printf("n = %d\n", n);
m = (int) Add3 (17.0);
printf("m = %d\n", m);
return 0;
}
/*********************************************
** Function: Add3
** Usage: x = Add3 (y);
**
** Input: an arbitrary double to which 3.0 is added
** Output: returns y + 3.0
*********************************************/
double Add3 (double y)
{
double z;
z = y + 3.0;
return (z);
}
When trying to compile
linux3[106] % gcc -Wall -ansi proto.c
proto.c: In function `main':
proto.c:22: warning: implicit declaration of function `Add3'
proto.c: At top level:
proto.c:43: warning: type mismatch with previous implicit declaration
proto.c:22: warning: previous implicit declaration of `Add3'
proto.c:43: warning: `Add3' was previously implicitly declared to return `int'
linux3[107] % a.out
x = 716910456.000000
n = 21
m = 7
linux3[108] %
The Fixed Program
The modified program works correctly when the function
prototype for Add3 is added.
/********************************************
** File: proto2.c
** Author: S. Bogar
** Date: 8/3/99
** Section: 101
** SSN: 123-45-6789
** EMail: bogar@cs.umbc.edu
**
** Demonstrate the need for prototypes
** This is the corrected version.
*********************************************/
#include
/* Include the prototype in this version */
double Add3(double);
int main ()
{
double x;
int n, m;
x = Add3 (5.0);
printf ("x = %f\n", x);
n = Add3 (10.0);
printf ("n = %d\n", n);
m = (int) Add3 (17.0);
printf ("m = %d\n", m);
return 0;
}
/*********************************************
** Function: Add3
** Usage: x = Add3 (y);
**
** Input: an arbitrary double, y, to which 3.0 is added
** Output: returns y + 3.0
*********************************************/
double Add3 (double y)
{
double z;
z = y + 3.0;
return (z);
}
The Sample Run
x = 8.000000
n = 13
m = 20
The Lesson
Function prototypes are essential in C.
The C compiler only makes one pass over the source code,
generating object code as it goes. So, it really needs to know the
type a function returns and the types of its arguments before it hits the first call to the function.
Some C compilers assume that functions for which there
is no prototype return type int . This assumption results
in an error message similar to the above when the compiler encounters the
function definition.