Overview
C declarations were designed so that the declaration of an object looks like the use of the object. This isn’t quite true - keywords like volatile
and const
only exist in declarations - but for the most part, this philosophy can be leveraged to read C declarations.
Declarators
A declarator in C is roughly an identifier along with pointers, function brackets, or array indications. Pointers will look like one of:
*
* const
* volatile
* const volatile
* volatile const
whereas direct declarators will look like one of:
identifier
identifier[size]
identifier(args)
(declarator)
Declarations
A declaration consists of at least one type-specifier (e.g. signed short
), storage class (e.g. static
), and/or type qualifier (e.g. const
) as well as one or more declarators.
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 - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.
- Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.