28.1
K&R-Style Functions
Classic C (also called K&R C after its authors, Brian Kernighan
and Dennis Ritchie) uses a function header that's
different from the one used in C++. In C++ the parameter types and
names are included inside the ( ) defining the function. In Classic
C, only the names appear. Type information comes later. The following
code shows the same function twice, first as defined in C++, followed
by its K&R definition:
int do_it(char *name, int function) // C++ function definition
{
// Body of the function
int do_it(name, function) // Classic C definition
char *name;
int function;
{
// Body of the function
When C++ came along, the ANSI C committee decided it would be a good
idea if C used the new function definitions. However, because there
was a lot of code out there using the old method, C accepts both
types of functions. C++ does not.
28.1.1 Prototypes
Classic C does not require
prototypes.
In many cases, prototypes are missing from C programs. A function
that does not have a prototype has an implied prototype of:
int funct( ); // Default prototype for Classic C functions
The ( ) in C does not denote an empty argument list. Instead it
denotes a variable length argument list with no type checking of the
parameters. Also, Classic C prototypes have no parameter lists. The
only "prototype"
you'll see consists merely of "(
)", such as:
int do_it( ); // Classic C function prototype
This tells C that do_it returns an int and takes any number of parameters. C does
not type-check parameters, so the following are legal calls to
do_it:
i = do_it( );
i = do_it(1, 2, 3);
i = do_it("Test", 'a');
C++ requires function prototypes, so you have to put them in. There
are tools out there such as the GNU prototize
utility that help you by reading your code and generating function
prototypes. Otherwise, you will have to do it manually.
|