What is the difference between a declaration and a definition in C++?

Last updated 21 June 2026

A declaration introduces a name and its type to the compiler; a definition additionally provides the full implementation or allocates storage. Every definition is also a declaration, but not every declaration is a definition.

  • Declaration:

    • Tells the compiler a name exists and what type it has, e.g. int f(int); or extern int x;.

    • You can declare the same entity multiple times.

  • Definition:

    • Provides the body or storage, e.g. int f(int){...} or int x;.

    • Subject to the One Definition Rule (ODR): exactly one definition across the program (except for inline functions and templates).

  • Practical impact:

    • Headers usually carry declarations; source files carry definitions, enabling separate compilation.

    • A missing definition produces a linker error (unresolved symbol), not a compiler error.

  • https://www.geeksforgeeks.org/compiler-design/difference-between-definition-and-declaration/
  • https://www.geeksforgeeks.org/cpp/cpp-interview-questions/
  • https://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration
  • https://www.cs.odu.edu/~zeil/cs333/f13/Public/faq/faq-htmlsu20.html
All C++ interview questions