Overview
A declaration specifies the interpretation and attributes of a set of identifiers. It indicates linkage, storage duration, and part of the type of the entities that the declarators denote. For example, the following declaration has two declarators x
and y
, both of type const int
, declared in file scope with static storage duration.
C declarations were designed so that the declaration of an object looks like the use of the object. This isn’t always true, but for the most part this philosophy can be leveraged to read them.
Declarators
A declarator in C is roughly an identifier along with pointers, function brackets, or array indications.
A declarator is said to be full if is not part of another declarator. If any part of a full declarator specifies a variable length array type, the declarator is said to be variably modified. Types containing variably modified declarators are likewise called variably modified types (VMTs).
Definitions
A definition is a declaration that causes storage to be reserved for the object (for object types) or includes the function body (for function types).
Initializers
An initializer is an expression that gives an object a value at time of declaration. Only variable-length arrays (VLAs) do not allow for an initializer. The default initializer looks like {0}
.
Precedence Rules
Declarations can be read by complying with the precedence rules outlined below:
- Find the name of the declaration.
- Obey the following precedence rules:
- Parentheses grouping together parts of a declaration
- Postfix operators
()
and[]
- Prefix operator: the asterisk
*
denoting “pointer to”
- If
const
and/orvolatile
keyword is next to a type specifier, it applies to the type specifier. Otherwise it applies to the pointer asterisk on its immediate left.
Bibliography
- Bryant, Randal E., and David O’Hallaron. Computer Systems: A Programmer’s Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.
- “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
- Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
- Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.