Declaration vs definition
Introduction
I was always confused when someone mentioned "declaration" or "definition" regarding a function or variable. Is the function definition the one with the body, or was that the declaration? I could never tell.
So let's answer this common question together.
Functions
A declaration refers to the function signature, while a definition refers to the function signature with its body.
To remember this more easily, I like to think of a function declaration as a tax declaration. When you declare your taxes, you are not bringing your actual money to the tax office so they can count it; you are just declaring the amount. Similarly, a function declaration does not specify how the function works; it only declares the function's "shape" and the types and sizes of its arguments.
int main(void);
On the other hand, a function definition is like defining what a car is. If you want to explain to someone what a car is, you need to describe how the engine works, how the fuel system works, and so on. You cannot just say "it has wheels and it moves." Similarly, the definition of a function explains its inner behavior: what it does and how it does it—something you cannot deduce from the function signature alone.
int main(void) { return 0; }
Now, why do we even have both definitions and declarations? Wouldn't it be easier to just have definitions?
Compilation in C works something like this:
Because of how the compilation process is structured, we need a way to handle the extern keyword. Without the ability to share a function's signature without its body, we couldn't properly reference functions across multiple translation units.
Imagine if we could only use a function's signature together with its body. Each translation unit using an external function would need to include the full definition. If there were multiple definitions across different units, keeping them consistent would be error-prone and cumbersome. Hence, we separate the signature (declaration) from the body (definition).
Variables
For variables, the distinction is simpler. A definition of a variable allocates storage for it, while a declaration simply specifies its type and name.
Declaration:
int32_t a; // 32 bits
Definition:
int32_t a = 5; // 32 bits, with a value
The case here is similar to functions. If you like analogies, it’s best to fully understand the function example first; then distinguishing declarations and definitions for variables becomes much easier.
Rules
Here are some important rules about declarations and definitions:
- There can be multiple declarations of the same function or variable in a translation unit, as long as they are identical.
- There must be exactly one definition for functions declared with
externor without any keyword. - There can be multiple definitions of functions declared with the
statickeyword, as long as they are in different translation units. - When declaring a
staticvariable, its value is assigned only once, as long as the value is a compile-time constant.