A declaration establishes the names and characteristics of data objects used in a program. A definition allocates storage for data objects, and associates an identifier with that object. When you declare or define a type, no storage is allocated.
The following table shows examples of declarations and definitions. The identifiers declared in the first column do not allocate storage; they refer to a corresponding definition. The identifiers declared in the second column allocate storage; they are both declarations and definitions.
Declarations | Declarations and definitions |
---|---|
extern double pi; | double pi = 3.14159265; |
struct payroll; |
struct payroll { char *name; float salary; } employee; |
Declarations determine the following properties of data objects and their identifiers:
The elements of a declaration for a data object are as follows:
In addition, for compatibility with GCC, XL C/C++ allows you to use attributes to modify the properties of data objects. Type attributes, which can be used to modify the definition of user-defined types, are described in Type attributes. Variable attributes, which can be used to modify the declaration of variables, are described in Variable attributes.
All declarations have the form:
Data declaration syntax .-----------------------------. .--------------------. V | V | >>---+-------------------------+-+----+----------------+-+------> '-storage_class_specifier-' '-type_qualifier-' .-,---------------------------. V | >--type_specifier----declarator--+-------------+-+--;---------->< '-initializer-'
Related information
A tentative definition is any external data declaration that has no storage class specifier and no initializer. A tentative definition becomes a full definition if the end of the translation unit is reached and no definition has appeared with an initializer for the identifier. In this situation, the compiler reserves uninitialized space for the object defined.
The following statements show normal definitions and tentative definitions.
int i1 = 10; /* definition, external linkage */ static int i2 = 20; /* definition, internal linkage */ extern int i3 = 30; /* definition, external linkage */ int i4; /* tentative definition, external linkage */ static int i5; /* tentative definition, internal linkage */ int i1; /* valid tentative definition */ int i2; /* not legal, linkage disagreement with previous */ int i3; /* valid tentative definition */ int i4; /* valid tentative definition */ int i5; /* not legal, linkage disagreement with previous */
C++ does not support the concept of a tentative definition: an external data declaration without a storage class specifier is always a definition.