Namespaces
Variants
Views
Actions

Difference between revisions of "c/language/declarations"

From cppreference.com
< c‎ | language
(+)
 
m (seealso C++; language links; minor grammar; "pointer-declarator" with hyphen does not appear in the n1570)
Line 28: Line 28:
 
For example,
 
For example,
 
{{source|1=
 
{{source|1=
int a, *b=NULL; // int is the type specifier,
+
int a, *b=NULL; // "int" is the type specifier,
                 // a is a declarator
+
                 // "a" is a declarator
                 // *b is a declarator and NULL is its initializer
+
                 // "*b" is a declarator and NULL is its initializer
const int *f(void); // int is the type specifier
+
const int *f(void); // "int" is the type specifier
                     // const is type type qualifier
+
                     // "const" is type type qualifier
                     // *f(void) is the declarator
+
                     // "*f(void)" is the declarator
enum COLOR {RED, GREEN, BLUE} c; // enum COLOR {RED, GREEN, BLUE} is the type specifier
+
enum COLOR {RED, GREEN, BLUE} c; // "enum COLOR {RED, GREEN, BLUE}" is the type specifier
                                 // c is the declarator
+
                                 // "c" is the declarator
 
}}
 
}}
  
The type of each identifier introduced in a declaration is determined by a combination of the type specified by the {{spar|type specifier}} and the type modifications applied by its {{spar|declarator}}
+
The type of each identifier introduced in a declaration is determined by a combination of the type specified by the {{spar|type specifier}} and the type modifications applied by its {{spar|declarator}}.
  
 
===Declarators===
 
===Declarators===
Line 51: Line 51:
 
{{sdsc|num=5| {{spar|noptr-declarator}} {{ttb|(}} {{spar|parameters-or-identifiers}} {{ttb|)}} }}
 
{{sdsc|num=5| {{spar|noptr-declarator}} {{ttb|(}} {{spar|parameters-or-identifiers}} {{ttb|)}} }}
 
{{sdsc end}}
 
{{sdsc end}}
@1@ the identifier that this declarator introduces
+
@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
+
@2@ any declarator may be enclosed in parentheses; this is required to introduce pointers to arrays and pointers to functions.
 
@3@ {{rlp|pointer|pointer declarator}}: the declaration {{c|S * cvr D}}; declares {{tt|D}} as a {{spar|cvr}}-qualified pointer to the type determined by {{tt|S}}.
 
@3@ {{rlp|pointer|pointer declarator}}: the declaration {{c|S * cvr D}}; declares {{tt|D}} as a {{spar|cvr}}-qualified pointer to the type determined by {{tt|S}}.
@4@ {{rlp|array|array declarator}}: the declaration {{c|S D[N]}} declares {{tt|D}} as an array of {{tt|N}} objects of the type determined by {{tt|S}}. {{spar|noptr-declarator}} is any other declarator except unparenthesized pointer-declarator.
+
@4@ {{rlp|array|array declarator}}: the declaration {{c|S D[N]}} declares {{tt|D}} as an array of {{tt|N}} objects of the type determined by {{tt|S}}. {{spar|noptr-declarator}} is any other declarator except unparenthesized pointer declarator.
@5@ {{rlp|function declaration|function declarator}}: the declaration {{c|S D(params)}} declared {{tt|D}} as a function taking the parameters {{tt|params}} and returning {{tt|S}} {{spar|noptr-declarator}} is any other declarator except unparenthesized pointer-declarator.
+
@5@ {{rlp|function declaration|function declarator}}: the declaration {{c|S D(params)}} declared {{tt|D}} as a function taking the parameters {{tt|params}} and returning {{tt|S}}. {{spar|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.
 
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.
Line 67: Line 67:
 
// declarator "obj" defines an object of type struct C
 
// declarator "obj" defines an object of type struct C
 
// declarator "*pObj" declares a pointer to C,
 
// declarator "*pObj" declares a pointer to C,
// initializer = &obj provides the initial value for that pointer
+
// initializer "= &obj" provides the initial value for that pointer
 
   
 
   
 
int a = 1, *p = NULL, f(void), (*pf)(double);
 
int a = 1, *p = NULL, f(void), (*pf)(double);
// the type specifier is int
+
// the type specifier is "int"
// declarator a defines an object of type int
+
// declarator "a" defines an object of type int
//  initializer =1 provides its initial value
+
//  initializer "=1" provides its initial value
// declarator *p defines an object of type pointer to int
+
// declarator "*p" defines an object of type pointer to int
//  initializer =NULL provides its initial value
+
//  initializer "=NULL" provides its initial value
// declarator f(void) declares a function taking void and returning int
+
// declarator "f(void)" declares a function taking void and returning int
// declarator (*pf)(double) defines an object of type pointer
+
// declarator "(*pf)(double)" defines an object of type pointer
 
//  to function taking double and returning int
 
//  to function taking double and returning int
 
   
 
   
Line 97: Line 97:
 
// The initializer "= NULL" provides the initial value of this pointer.
 
// 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
+
// If "foo" is used in an expression of the form of the declarator, its type would be
 +
// int.
 
int x = (*(*foo)(1.2))[0];
 
int x = (*(*foo)(1.2))[0];
 
}}
 
}}
Line 106: Line 107:
 
A ''definition'' is a declaration that provides all information about the identifiers it declares.
 
A ''definition'' is a declaration that provides all information about the identifiers it declares.
  
Every declaration of a {{rlp|enum}} or a {{rlp|typedef}} is a definition.
+
Every declaration of an {{rlp|enum}} or a {{rlp|typedef}} is a definition.
  
 
For functions, a declaration that includes the function body is a {{rlp|function definition}}:
 
For functions, a declaration that includes the function body is a {{rlp|function definition}}:
Line 138: Line 139:
 
static int n; // OK
 
static int n; // OK
 
}}
 
}}
* Non-VLA {{rlp|typedef}} can be repeated as long as it names the same type
+
* Non-VLA {{rlp|typedef}} can be repeated as long as it names the same type:
 
{{source|1=
 
{{source|1=
 
typedef int int_t;  
 
typedef int int_t;  
 
typedef int int_t; // OK
 
typedef int int_t; // OK
 
}}
 
}}
* {{rlp|struct}} and {{rlp|union}} declarations can be repeated
+
* {{rlp|struct}} and {{rlp|union}} declarations can be repeated:
 
{{source|1=
 
{{source|1=
 
struct X;
 
struct X;
Line 163: Line 164:
 
{{rev begin}}
 
{{rev begin}}
 
{{rev|since=c99|
 
{{rev|since=c99|
If any part of a declarator is a {{rlp|array|VLA array}} declarator, the entire declarator's type is known as "variably-modified type". Types defined from variably-modified types are also variably-modified.
+
If any part of a declarator is a {{rlp|array|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 {{rlp|scope|block scope}} or function prototype scope and cannot be members of structs or unions. Although {{rlp|array|VLA}} can only have automatic {{rlp|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 {{rlp|goto}}, {{rlp|switch}}. {{rlp|setjmp}}
 
Declarations of any variably-modified types may appear only at {{rlp|scope|block scope}} or function prototype scope and cannot be members of structs or unions. Although {{rlp|array|VLA}} can only have automatic {{rlp|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 {{rlp|goto}}, {{rlp|switch}}. {{rlp|setjmp}}
Line 174: Line 175:
 
}}
 
}}
 
{{rev end}}
 
{{rev end}}
 +
 +
===See also===
 +
{{dsc begin}}
 +
{{dsc see cpp | cpp/language/declarations | Declarations}}
 +
{{dsc end}}
 +
 +
[[ar:cpp/language/declarations]]
 +
[[cs:cpp/language/declarations]]
 +
[[de:cpp/language/declarations]]
 +
[[es:cpp/language/declarations]]
 +
[[fr:cpp/language/declarations]]
 +
[[it:cpp/language/declarations]]
 +
[[ja:cpp/language/declarations]]
 +
[[ko:cpp/language/declarations]]
 +
[[pl:cpp/language/declarations]]
 +
[[pt:cpp/language/declarations]]
 +
[[ru:cpp/language/declarations]]
 +
[[tr:cpp/language/declarations]]
 +
[[zh:cpp/language/declarations]]

Revision as of 03:43, 22 January 2015

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

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.

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 stucts and unions, declarations that specify the list of members are definitions:

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

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.

Notes

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

(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. setjmp

(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)

See also

C++ documentation for Declarations