The general syntax of a declaration is as follows:
declaration:
declaration-specifiers init-declarator-list(opt);
declaration-specifiers:
storage-class-specifier declaration-specifiers(opt)
type-specifier declaration-specifiers(opt)
type-qualifier declaration-specifiers(opt)
init-declarator-list:
init-declarator
init_declarator-list , init-declarator
init-declarator:
declarator
declarator = initializer
Note the following items about the general syntax of a declaration:
- The storage-class-specifier, type-
qualifier, and type-specifier can be listed in any
order. All are optional, but, except for function declarations,
at least one such specifier or qualifier must be present. Placing
the storage-class-specifier anywhere but at the beginning of the
declaration is an obsolete style.
- Storage-class keywords are
auto
, static
, extern
, and
register
.
- Type qualifiers are
const
and
volatile
.
- The declarator is the name of the object
or function being declared. A declarator can be as simple
as a single identifier, or can be a complex construction
declaring an array, structure, pointer, union, or function
(such as
*x
, tree()
, and
treebar[10]
).
- Initializers are optional and provide the initial
value of an object. Initializers can be a single value or a
brace-enclosed list of values, depending on the type of object
being declared.
- A declaration determines the beginning of an identifier's
scope.
- An identifier's linkage is determined by the declaration's
placement and its specified storage class.
Consider the following example:
volatile static int data = 10;
This declaration shows a qualified type (a data type with a
type qualifier-in this case, int
qualified by
volatile
), a storage class (static
), a
declarator (data
), and an initializer (10
). This declaration is also a definition, because storage is
reserved for the data object data
.
The previous example is simple to interpret, but complex
declarations are more difficult. See your platform-specific DEC C documentation for more information about
interpreting C declarations.
The following semantic rules apply to declarations:
- Empty declarations are illegal; declarations must contain
at least one declarator, or specify a structure tag, union tag,
or the members of an enumeration.
- Each declarator declares one identifier. There is no limit
to the number of declarators in a declaration.
- At most, one storage-class specifier can be used in each
object declaration. If none is provided, the
auto
storage class is assigned to objects declared inside a function
definition, and the extern
class is assigned to
objects declared outside of a function definition.
- The only allowable (and optional) storage class for
declaration of a function with block scope is
extern
.
- If no type-specifier is present, the default is
signed int
.
- A declarator is usable only over a certain range of the
program, determined by the declarator's scope. The duration
of its storage allocation is dependent on its storage class.
See Section 2.3 for more information on
scope and Section 2.10 for more
information on storage classes.
- The usefulness of an identifier can be limited by its
visibility, which can be hidden in some parts of the program. See
Section 2.4 for more information on
visibility.
- All declarations in the same scope that refer to the same
object or function must have compatible types.
- If an object has no linkage, there can be no more
than one declaration of the object with the same scope and
in the same name space. Objects without linkage must have
their type completed by the end of the declaration, or by
the final initializer (if it has one). Section 2.8 describes linkage.
Storage Allocation
Storage is allocated to a data object in the following
circumstances:
- If the object has no linkage, storage is allocated
upon declaration of the object. If a block scope object with
auto
or register
storage class is
declared, storage is deallocated at the end of the block.
- If the object has internal linkage, storage is allocated
upon the first definition of the object.
- If the object has external linkage, storage is allocated
upon initialization of the object, which must occur only once
for each object. If an object has only a tentative definition
(see Section 2.9), the
compiler acts as though there were a file scope definition of
the object with an initializer of zero. Section 2.8 describes linkage in detail.
Previous Page | Next Page | Table of Contents | Index