Programming
Coding Basics
Definition vs Declaration
Computer Science
Language Syntax

What is the difference between a definition and a declaration?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In programming, particularly in languages like C, C++, and similar derivative languages, distinguishing between a declaration and a definition is pivotal for writing clear, effective, and error-free code. The confusion between these two terms often arises because they are subtly interlinked, yet they perform different roles within the language.

Declaration vs. Definition

Declaration

A declaration introduces one or more identifiers (such as variables, functions, classes, etc.) and specifies their type, but it does not allocate memory for them (except for extern variables). A declaration essentially tells the compiler about the existence and the type of the identifiers without fully describing how they are implemented. Declarations are crucial because they provide information to the compiler that helps it ensure that the identifiers are used consistently throughout the program.

Example:

c
extern int x; // Declaration of a variable
int f(int, int); // Declaration of a function
class A; // Forward declaration of a class

In each of these examples, the compiler learns about the name and type of the variable, function, or class, but there is no allocation of memory (for variables) or provision of code (for functions or classes).

Definition

A definition, on the other hand, does everything a declaration does and also allocates memory (for variables) or provides the implementation (for functions or classes). It fully describes the identifier. When you define a variable, a function, or a class, you are giving all the details that the compiler needs to allocate space for that variable or to compile and link calls to that function or class.

Example:

c
int x; // Definition of a variable, memory is allocated
int f(int a, int b) { return a + b; } // Definition of a function, code is provided
class A { public: int i; }; // Definition of a class

Here, each identifier is not only declared but defined. The variable x is allocated memory, the function f is given an implementation, and the class A is fully specified.

Key Differences

The essential differences can be highlighted in a table:

AspectDeclarationDefinition
PurposeIntroduces the identifier and typeFully describes and allocates identifier
Memory AllocationNo memory allocation (except extern)Allocates memory (variables)
ImplementationNo implementation detailsProvides implementation details
Multiple Declarations/DefinitionsMultiple declarations allowedMultiple definitions can lead to errors
LinkageUsed for linking across multiple filesUsually contained within a single file

Further Considerations

Forward Declarations
  • Forward declarations are a type of declaration that is used particularly with classes. It allows the use of the class name before its full definition is known. This is useful for resolving circular dependencies between classes.

Example:

c
1class B; // Forward declaration
2
3class A {
4    B* bPtr; // OK: pointer to B, despite B not being fully defined
5};
6
7class B {
8    A* aPtr; // OK using forward declaration
9};
Extern Keyword
  • The extern keyword is used to declare a variable without defining it. It is especially useful in managing global variables across multiple files.

Example:

c
1// File1.cpp
2extern int globalVar; // Declaration, no memory allocation
3
4// File2.cpp
5int globalVar; // Definition, memory is allocated

Understanding these nuances between declarations and definitions not only assists in preventing linker errors but also improves code organization and readability. By strategically using declarations and definitions, programmers can manage larger codebases more effectively, promoting modular design and cleaner interfaces between code modules.


Course illustration
Course illustration

All Rights Reserved.