Declarations/Definitions


When code has been compiled to object form, a facility is required to expose the interface to the code contained therein to other translation units. A declaration introduces the name of a type or object to the compiler. For example, a header file may declare a function without specifying the body of that function. By using declarations, a system may be built in a modular fashion, whereby it consists of many translation units, the contents of which are published via header files.

A declaration is also a definition unless:

Examples

The following are declarations but not definitions.

exter int i;                    // external with no initializer therefore declaration
int f(int, char*);              // no function body therefore declaration
class test;                     // class name, elaborated type - declaration
typedef unsigned short ushort;  // typedefs are declarations

The following are both declarations and definitions.

extern int e=0;                    // external but with initializer therefore definition
int i;                             // default is definition (declared without external).
int Increment(int x) {return ++x;} // function body therefore definition
class test {int i;};               // class with body, therefore definition
enum {value1,value2};              // enumerated type definition

Notes