C Tutorial Prototypes
main()
Multiple Files
Prototypes

Preprocessor
#define
#include
foo.h vs foo.c
#if
Multiple #includes -- #pragma once
Assert 
 
 

 

HOME
 

A "prototype" for a function gives its name and arguments but not its body. In order for a

caller, in any file, to use a function, the caller must have seen the prototype for that

function. For example, here's what the prototypes would look like for Twice() and Swap(). The function body is absent and there's a semicolon (;) to terminate the prototype...

 

int Twice(int num);

void Swap(int* a, int* b);

 

 

In pre-ANSI C, the rules for prototypes where very sloppy -- callers were not required to see prototypes before calling functions, and as a result it was possible to get in situations where the compiler generated code which would crash horribly.

 

In ANSI C, I'll oversimplify a little to say that...

 

1) a function may be declared static in which case it can only be used in the same file where it is used below the point of its declaration. Static functions do not require a separate prototype so long as they are defined before or above where they are called which saves some work.

 

2) A non-static function needs a prototype. When the compiler compiles a function definition, it must have previously seen a prototype so that it can verify that the two

are in agreement ("prototype before definition" rule). The prototype must also be seen

by any client code which wants to call the function ("clients must see prototypes"

rule).(The require-prototypes behavior is actually somewhat of a compiler option, but

it's smart to leave it on.)

Want To Know more with

Video ???

Contact for more learning: webmaster@freehost7com