Namespaces
Variants
Views
Actions

Declarations

From cppreference.com
< c‎ | language

A declaration is a C language construct that introduces one or more identifiers into the program and specifies their meaning and properties.

Declarations may appear in any scope. Each declaration ends with a semicolon (just like a statement) and consists of two distinct parts:

specifiers-and-qualifiers declarators-and-initializers ;

where

specifiers-and-qualifiers - whitespace-separated list of, in any order,
  • exactly one type specifier:


declarators-and-initializers - comma-separated list of declarators (each declarator provides additional type information and/or the identifier to declare). Declarators may be accompanied by initializers. The enum, struct, and union declarations may omit declarators, in which case they only introduce the enumeration constants and/or tags.


For example,

int a, *b=NULL; // "int" is the type specifier,
                // "a" is a declarator
                // "*b" is a declarator and NULL is its initializer
const int *f(void); // "int" is the type specifier
                    // "const" is type type qualifier
                    // "*f(void)" is the declarator
enum COLOR {RED, GREEN, BLUE} c; // "enum COLOR {RED, GREEN, BLUE}" is the type specifier
                                 // "c" is the declarator

The type of each identifier introduced in a declaration is determined by a combination of the type specified by the type specifier and the type modifications applied by its declarator.

Contents

[edit] Declarators

Each declarator is one of the following:

identifier (1)
( declarator ) (2)
* qualifiers(optional) declarator (3)
noptr-declarator [ static(optional) qualifiers(optional) expression ]

noptr-declarator [ qualifiers(optional) * ]

(4)
noptr-declarator ( parameters-or-identifiers ) (5)
1) the identifier that this declarator introduces.
2) any declarator may be enclosed in parentheses; this is required to introduce pointers to arrays and pointers to functions.
3) pointer declarator: the declaration S * cvr D; declares D as a cvr-qualified pointer to the type determined by S.
4) array declarator: the declaration S D[N] declares D as an array of N objects of the type determined by S. noptr-declarator is any other declarator except unparenthesized pointer declarator.
5) function declarator: the declaration S D(params) declared D as a function taking the parameters params and returning S. noptr-declarator is any other declarator except unparenthesized pointer declarator.

The reasoning behind this syntax is that when the identifier declared by the declarator appears in an expression of the same form as the declarator, it would have the type specified by the type specifier sequence.

struct C {
    int member; // "int" is the type specifier 
                // "member" is the declarator
} obj, *pObj = &obj;
// "struct C { int member; }" is the type specifier
// declarator "obj" defines an object of type struct C
// declarator "*pObj" declares a pointer to C,
// initializer "= &obj" provides the initial value for that pointer
 
int a = 1, *p = NULL, f(void), (*pf)(double);
// the type specifier is "int"
// declarator "a" defines an object of type int
//   initializer "=1" provides its initial value
// declarator "*p" defines an object of type pointer to int
//   initializer "=NULL" provides its initial value
// declarator "f(void)" declares a function taking void and returning int
// declarator "(*pf)(double)" defines an object of type pointer
//   to function taking double and returning int
 
int (*(*foo)(double))[3] = NULL;
// the type specifier is int
// 1. declarator "(*(*foo)(double))[3]" is an array declarator:
//    the type declared is "/nested declarator/ array of 3 int"
// 2. the nested declarator is "*(*foo)(double))", which is a pointer declarator
//    the type declared is "/nested declarator/ pointer to array of 3 int"
// 3. the nested declarator is "(*foo)(double)", which is a function declarator
//    the type declared is "/nested declarator/ function taking double and returning
//        pointer to array of 3 int"
// 4. the nested declarator is "(*foo)" which is a (parenthesized, as required by
//        function declarator syntax) pointer declarator.
//    the type declared is "/nested declarator/ pointer to function taking double
//        and returning pointer to array of 3 int"
// 5. the nested declarator is "foo", which is an identifier.
// The declaration introduces the identifier "foo" to refer to an object of type
// "pointer to function taking double and returning pointer to array of 3 int"
// The initializer "= NULL" provides the initial value of this pointer.
 
// If "foo" is used in an expression of the form of the declarator, its type would be
// int.
int x = (*(*foo)(1.2))[0];

The end of every declarator that is not part of another declarator is a sequence point.

[edit] Definitions

A definition is a declaration that provides all information about the identifiers it declares.

Every declaration of an enum or a typedef is a definition.

For functions, a declaration that includes the function body is a function definition:

int foo(double); // declaration
int foo(double x){ return x; } // definition

For objects, a declaration that allocates storage (automatic or static, but not extern) is a definition, while a declaration that does not allocate storage (external declaration) is not.

extern int n; // declaration
int n = 10; // definition

For structs and unions, declarations that specify the list of members are definitions:

struct X; // declaration
struct X { int n; }; // definition

[edit] Redeclaration

A declaration cannot introduce an identifier if another declaration for the same identifier in the same scope appears earlier, except that

  • Declarations of objects with linkage (external or internal) can be repeated:
extern int x;
int x = 10; // OK
extern int x; // OK
 
static int n;
static int n = 10; // OK
static int n; // OK
  • Non-VLA typedef can be repeated as long as it names the same type:
typedef int int_t; 
typedef int int_t; // OK
struct X;
struct X { int n; };
struct X;

These rules simplify the use of header files.

[edit] Notes

In C89, declarations within any compound statement (block scope) must appear in the beginning of the block, before any statements.

Also, in C89, functions returning int may be implicitly declared by the function call operator and function parameters of type int do not have to be declared when using old-style function definitions.

(until C99)

Empty declarators are prohibited; a declaration must be a static_assert declaration or (since C11) have at least one declarator or declare at least one struct/union/enum tag, or introduce at least one enumeration constant.

If any part of a declarator is a VLA array declarator, the entire declarator's type is known as "variably-modified type". Types defined from variably-modified types are also variably modified (VM).

Declarations of any variably-modified types may appear only at block scope or function prototype scope and cannot be members of structs or unions. Although VLA can only have automatic storage duration, a VM type such as a pointer to a VLA may be static. There are other restrictions on the use of VM types, see goto, switch. longjmp

(since C99)

static_asserts are considered to be declarations from the point of view of the C grammar (so that they may appear anywhere a declaration may appear), but they do not introduce any identifiers and do not follow the declaration syntax.

(since C11)

[edit] References

  • C11 standard (ISO/IEC 9899:2011):
  • 6.7 Declarations (p: 108-145)
  • C99 standard (ISO/IEC 9899:1999):
  • 6.7 Declarations (p: 97-130)
  • C89/C90 standard (ISO/IEC 9899:1990):
  • 3.5 Declarations

[edit] See also

C++ documentation for Declarations