|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
Declarations introduce new names into a program. Topics covered in this section include the following uses for declarations:
- Specify storage-class, type, and linkage for an object or function.
- Declare a function as inline or virtual.
- Qualify a declaration as const or volatile.
- Associate a name with a constant (enumeration declaration).
- Declare a new type (class, struct, or union declaration).
- Specify a synonym for a type (typedef declaration).
- Specify a namespace.
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.
- decl-specifiersopt declarator-listopt ;
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.
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 allocation of storage and appropriate initializations for that object.