| Basic concepts | ||||
| Keywords | ||||
| Preprocessor | ||||
| Statements | ||||
| Expressions | ||||
| Initialization | ||||
| Declarations | ||||
| Functions | ||||
| Miscellaneous | ||||
| History of C | ||||
| Technical Specifications |
Adeclaration is a C language construct that introduces one or moreidentifiers into the program and specifies their meaning and properties.
Declarations may appear in any scope. Each declaration ends with a semicolon (just likea statement) and consists oftwo(until C23)three(since C23) distinct parts:
specifiers-and-qualifiersdeclarators-and-initializers (optional); | (1) | ||||||||
attr-spec-seqspecifiers-and-qualifiersdeclarators-and-initializers; | (2) | (since C23) | |||||||
attr-spec-seq; | (3) | (since C23) | |||||||
where
| specifiers-and-qualifiers | - | whitespace-separated list of, in any order,
|
| declarators-and-initializers | - | comma-separated list ofdeclarators (each declarator provides additional type information and/or the identifier to declare). Declarators may be accompanied byinitializers. Theenum,struct, andunion declarations may omitdeclarators, in which case they only introduce the enumeration constants and/or tags. |
| attr-spec-seq | - | (C23)optional list ofattributes, applied to the declared entities, or forms an attribute declaration if appears alone. |
For example,
int a,*b=NULL;// "int" is the type specifier,// "a" is a declarator// "*b" is a declarator and NULL is its initializerconstint*f(void);// "int" is the type specifier// "const" is the type qualifier// "*f(void)" is the declaratorenum 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 thetype specifier and the type modifications applied by itsdeclarator.The type of a variable might also be inferred ifauto specifier is used.(since C23)
Attributes(since C23) may appear inspecifiers-and-qualifiers, in which case they apply to the type determined by the preceding specifiers.
Contents |
Each declarator is one of the following:
| identifierattr-spec-seq (optional) | (1) | ||||||||
(declarator) | (2) | ||||||||
*attr-spec-seq (optional)qualifiers (optional)declarator | (3) | ||||||||
noptr-declarator[static(optional)qualifiers (optional)expression]noptr-declarator | (4) | ||||||||
noptr-declarator(parameters-or-identifiers) | (5) | ||||||||
D as acvr-qualified pointer to the type determined byS.D as an array ofN objects of the type determined byS.noptr-declarator is any other declarator except unparenthesized pointer declarator.D as a function taking the parametersparams and returningS.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 asequence point.
In all cases,attr-spec-seq is an optional sequence ofattributes(since C23). When appearing immediately after the identifier, it applies to the object or function being declared.
Adefinition is a declaration that provides all information about the identifiers it declares.
Every declaration of anenum or atypedef is a definition.
For functions, a declaration that includes the function body is afunction definition:
int foo(double);// declarationint 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.
externint n;// declarationint n=10;// definition
Forstructs andunions, declarations that specify the list of members are definitions:
struct X;// declarationstruct X{int n;};// definition
A declaration cannot introduce an identifier if another declaration for the same identifier in the samescope appears earlier, except that
externint x;int x=10;// OKexternint x;// OK staticint n;staticint n=10;// OKstaticint n;// OK
typedefint int_t;typedefint int_t;// OK
struct X;struct X{int n;};struct X;
These rules simplify the use of header files.
In C89, declarations within anycompound statement (block scope) must appear in the beginning of the block, before anystatements. Also, in C89, functions returningint may be implicitly declared by thefunction call operator and function parameters of typeint do not have to be declared when using old-stylefunction definitions. | (until C99) |
Empty declarators are prohibited; a simple declaration must 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 avariable-length array (VLA) 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 atblock scope or function prototype scope and cannot be members of structs or unions. Although VLA can only have automatic or allocatedstorage duration, a VM type such as a pointer to a VLA may be static. There are other restrictions on the use of VM types, seegoto,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) |
Attribute declarations are also considered to be declarations (so that they may appear anywhere a declaration may appear), but they do not introduce any identifiers. A single | (since C23) |
C++ documentation forDeclarations |