Overview of data declarations and definitions
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 { |
- Scope, which describes the region of program text in which an identifier can be used to access its object
- Visibility, which describes the region of program text from which legal access can be made to the identifier's object
- Duration, which defines the period during which the identifiers have real, physical objects allocated in memory
- Linkage, which describes the correct association of an identifier to one particular object
- Type, which determines how much memory is allocated to an object and how the bit patterns found in the storage allocation of that object should be interpreted by the program
- Storage class specifiers, which specify storage duration and linkage
- Type specifiers, which specify data types
- Type qualifiers, which specify the mutability of data values
- Declarators, which introduce and include identifiers
- Initializers, which initialize storage with initial values
In addition, for compatibility with GCC, z/OS® XL C/C++ allows you to use attributes to modify the properties of data objects. They are described in Variable attributes (IBM extension).
All declarations have the form:
Data declaration syntax
Tentative definitions
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.
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.