What is the difference between declaration and definition?
Short answer: A declaration tells the compiler that a name exists and what its type is. A definition is a declaration that also creates the thing: it allocates storage for a variable or provides the body of a function. You can declare something many times, but you must define it exactly once in the whole program.
extern int baud; /* declaration only: storage is elsewhere */
int baud = 115200; /* definition (allocates storage) */
int add(int a, int b); /* declaration (prototype) */
int add(int a, int b) { return a + b; } /* definition */
struct packet; /* declaration of an incomplete type */
struct packet { uint8_t len; uint8_t data[32]; }; /* definition of the type */
Where they go: declarations in headers (uart.h), definitions in exactly one source file (uart.c). Every file that includes the header then agrees on the type, and the linker finds the single definition.
Tentative definitions (a C quirk): int x; at file scope without an initialiser is a tentative definition. Older compilers merged identical tentative definitions from several files into one (the "common" model). GCC 10 changed the default to -fno-common, so the same code now fails with "multiple definition of x". The fix is the proper pattern: extern int x; in the header and int x; in one .c file.
Errors you get when you get it wrong
| Mistake | Error |
|---|---|
| declared but never defined, and used | linker: undefined reference to 'baud' |
| defined in two files | linker: multiple definition of 'baud' |
| declarations with different types in different files | often no error at all, just wrong behaviour (undefined behaviour) |
The last row is why every .c file should include its own header: the compiler then checks that the definition matches the declaration.