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 {
                  char *name;
                  float salary;
               } employee;
Note: C only The C99 standard no longer requires that all declarations appear at the beginning of a function before the first statement. C++ only As in C++, you can mix declarations with other statements in your code.
Declarations determine the following properties of data objects and their identifiers:
  • 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
The elements of a declaration for a data object are as follows:

IBM extension begins 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). IBM extension ends

All declarations have the form:

Data declaration syntax


1? +  storage_class_specifier? +  type_qualifier  type_specifier + , declarator? initializer  ;

Tentative definitions

C only 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.

C only 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++ only C++ does not support the concept of a tentative definition: an external data declaration without a storage class specifier is always a definition.