Short answer
A declaration tells the compiler that a name exists and what kind of thing it is: a variable, a function, or a type. The declaration gives the name a type, so the compiler knows how to treat every later use of it. A declaration does not always create anything in memory. That is the main difference from a definition.
int count; /* declares count as an int; at file scope this also creates it */
extern int limit; /* declares limit as an int; it is created somewhere else */
int add(int a, int b); /* declares add as a function taking two ints and returning an int */
The type can be one of the primitive data types in c, such as int, char, or double, or a type the program defines.
Declaration versus definition
A definition is a declaration that also creates the thing it names:
- For a variable, a definition reserves memory for its value.
- For a function, a definition provides the body, which is the code that runs when the function is called.
A function prototype is a declaration with no body:
int add(int a, int b); /* declaration: no body */
int add(int a, int b) { /* definition: has a body */
return a + b;
}
For variables, extern marks a declaration that does not create the variable. A program that spans two files shows the pattern:
/* counter.c */
int counter = 0; /* definition: reserves memory for counter */
/* main.c */
extern int counter; /* declaration: counter is defined in another file */
The linker joins the two files and connects the use of counter in main.c to the single definition in counter.c.
Why declarations come first
The compiler reads a file from top to bottom. When it sees a name, it needs to know what that name is. Using a function before any declaration is an error in modern C:
int main(void) {
return square(4); /* square has not been declared yet */
}
int square(int n) {
return n * n;
}
Recent compilers report an error for this code. Older compilers accepted it with a warning and guessed the function’s type, which could cause bugs. The fix is to declare the function before main:
int square(int n); /* declaration: tells the compiler about square */
int main(void) {
return square(4);
}
int square(int n) {
return n * n;
}
Declarations in header files
Larger programs split their code across several source files. Declarations usually go in a header file with a .h extension, and each source file includes that header. The definitions stay in one source file, so each name is defined exactly once.
/* mathutil.h */
#ifndef MATHUTIL_H
#define MATHUTIL_H
int add(int a, int b); /* declaration only */
#endif
/* mathutil.c */
#include "mathutil.h"
int add(int a, int b) { /* definition */
return a + b;
}
The #ifndef lines are a header guard, which stops the header from being processed twice in the same file. A full header and source pair is shown in c header file example.
Function prototypes are declarations too. They give the return type and parameter types, which lets the compiler check each call. The idea is explained further in what is a prototype in c.
The C++ view
C++ uses the same ideas. It also has class declarations. A forward declaration such as class Widget; names a type without defining its members. A header can use that name when it only needs to refer to the type, which keeps the header smaller.
Common mistakes
- Declaring a function and never defining it. The call compiles, but the linker reports an undefined reference when it builds the program.
- Declaring the same name with different types. Two files that disagree about a variable’s type can cause linker errors or undefined behavior.
- Using
externwithout a definition. A variable declared withexternmust be defined in exactly one place. - Assuming a header defines things. A header normally declares names. The definitions go in a source file.
Summary
- A declaration gives a name and a type. It may not create storage.
- A definition is a declaration that creates a variable or provides a function body.
- Declare names before use, usually in header files, and keep one definition for each name.