Chapter 6 Declarations

Declarations introduce new names into a program. They can be used to:

Specify storage class, type, and linkage for an object

Specify storage class, type, and linkage for a function

Provide the definition of a function

Provide an initial value for an object

Associate a name with a constant (enumerated type declaration)

Declare a new type (class, struct, or union declaration)

Specify a synonym for a type (typedef declaration)

In addition to introducing a new name, a declaration specifies how an identifier is to be interpreted by the compiler. Declarations do not automatically reserve storage associated with the identifier—reserving storage is done by definitions.

Note :

Most declarations are also definitions.

Syntax

declaration:
decl-specifiersoptdeclarator-listopt;
function-definition
linkage-specification

The declarators in declarator-list contain the names being declared. Although the declarator-list is shown as optional, it can be omitted only in declarations or definitions of a function.

Note :

The declaration of a function is often called a “prototype.” This declaration provides type information about arguments and the function's return type that allows the compiler to perform correct conversions and to ensure type safety.

The decl-specifiers part of a declaration is also shown as optional; however, it can be omitted only in declarations of class types or enumerations.

Declarations occur in a scope. This controls the visibility of the name declared and the duration of the object defined (if any). For more information about how scope rules interact with declarations, see “Scope” in Chapter 2, on topic .

An object declaration is also a definition unless it contains the extern storage-class specifier described in “Storage-Class Specifiers”. A function declaration is also a definition unless it is a prototype–a function header with no defining function body. An object's definition causes the storage for that object to be allocated and appropriate initializations to take place.