When we begin learning a programming language, we often come across two important terms: declaration and definition. For beginners, these concepts can seem confusing because both are closely related and are frequently used together in programs.
Many programmers initially struggle to understand the actual difference between declaration and definition. However, with practice and hands-on coding experience, these concepts gradually become clear and easier to use correctly.
Understanding declaration and definition is very important because they form the foundation of how variables, functions, and memory management work in C and C++ programming.
In this blog post, you will learn the difference between declaration and definition with the help of simple explanations and practical programming examples.
So, let’s get started.
Declaration and Definition:
Let’s understand declaration and definition one by one with the help of simple coding examples.
Declaration:
In C and C++, an identifier must be declared before it is used. Why am I saying this? Let’s understand this with an example.
The following program tries to print the value of count:
#include<stdio.h>
int main()
{
printf("%d\n", count);
return 0;
}
Now the question is: what will happen when this code is compiled?
The compiler will generate an error during the compilation process because it has no information about the identifier count.
A typical compiler error may look like this:
error: ‘count’ undeclared
During compilation, the compiler must know:
- The name of the identifier.
- The type of the identifier.
- How the identifier should be used
Since count was never declared, the compiler cannot process the statement correctly.
To fix this issue, we need to declare the variable before using it:
#include <stdio.h>
int main()
{
int count = 10;
printf("%d\n", count);
return 0;
}
Output: 10
Now the compiler knows that count is an integer variable.
What is a Declaration?
A declaration introduces an identifier to the compiler and specifies its type.
It tells the compiler:
- The identifier exists.
- The type of the identifier.
- How the identifier can be used in the program
A declaration allows the compiler to process references to that identifier correctly.
For example:
extern int data; extern int fun(int, int); double foo(int, double);
Here:
- data is declared as an integer variable
- fun() is declared as a function returning int
- foo() is declared as a function returning double
For example:
extern int data;
This is only a declaration. It tells the compiler that data exists somewhere else in the program.
However:
int count = 10;
This is both:
- A declaration.
- And a definition
Because it also allocates storage for the variable.
Definition:
A definition is the actual implementation or instantiation of an identifier. Unlike a declaration, a definition provides complete information about the identifier and allocates storage for it (in the case of variables). The linker depends on definitions to resolve symbols correctly during the linking stage.
The following are definitions corresponding to typical declarations:
int data;
int fun(int a, int b)
{
return (a+b);
}
double foo(int a, double b)
{
return (a*b);
}
Here:
- data is defined as an integer variable.
- fun() is defined as a function that returns the sum of two integers.
- foo() is defined as a function that returns the product of an integer and a double
Storage Allocation:
In C and C++, storage is allocated when a variable is defined. Consider the following:
int value; char cValue; int test = 0;
Each of these statements is both a declaration and a definition, the compiler introduces the identifier and immediately allocates memory for it. Note that test is also initialized to 0, which is a separate concept from definition.
Key Distinction: Declaration vs Definition:
I have already written a blog post (Difference between declaration and definition) about this, and I think you should check it out as well.
But for now, let’s understand the key difference between them.
Most definitions are also declarations because they introduce an identifier and provide complete information about it. However, not every declaration is a definition.
For example:
extern int data;
This is only a declaration. It informs the compiler that data exists somewhere else, but no storage is allocated for it.
To summarize:
- Every definition is also a declaration.
- But not every declaration is a definition
Declaration Scope:
In C, every identifier (such as a variable, function, typedef name, or macro) is visible only within a certain region of the program. This region is called the scope of the identifier.
In simple words, scope determines where an identifier can be accessed in the program.
C provides different types of scope, such as:
- block scope.
- file scope.
- function scope.
- function prototype scope.
We will not discuss all of them in detail here. The important point to understand is that an identifier is only valid within the scope where it is declared.
Same Identifier in the Same Scope:
You cannot declare two variables with the same name in the same scope.
For example:
#include<stdio.h>
int main()
{
int count = 10;
int count = 0; //error
printf("%d\n", count);
return 0;
}
Here, both variables named count are declared in the same block scope, so the compiler will generate a redefinition error.
Same Identifier in Different Scopes:
However, you can use the same identifier name in different scopes.
Example:
#include <stdio.h>
int count = 10; // File scope variable
int main()
{
int count = 0; // Local variable with block scope
printf("%d\n", count);
return 0;
}
This program is valid because the two variables belong to different scopes.
In this case:
- The global count has file scope.
- The local count inside main() has block scope
The local variable hides the global variable inside the block.
Output: 0
Declaration and Definition: From the Compiler and Linker Perspective:
One of the easiest ways to understand the difference between a declaration and a definition is to see how the compiler and linker use them differently.
| Factor | Compiler | Linker |
|---|---|---|
| Needs | Declarations | Definitions |
| Purpose | Type checking and symbol validation | Symbol resolution and address binding |
| Reports Errors Like | Undeclared Identifier | Undefined reference or multiple definition |
| Works At | Compile time | Link time |
| Produces | Object files | Executable or library |
Important points:
- A declaration tells the compiler that something exists.
- A definition provides the actual implementation or storage.
Recommended Articles for you:
- Introduction of internal, external, and none linkage in C
- Best Keyboards for programmers and Gamers.
- Best electronic kits for programmers.
- Memory Layout of C program.
- C Identifier and naming rules.
- C variables with examples.
- Storage class specifiers with example code.
- How to access a global variable if there is a local variable with the same name.
- Can access local variables beyond its scope?
- Use of typedef in C
- C macro, you should know
- Best gift for programmers.
- List of some best laptops for programmers and gammers.
- typedef vs #define in c, you should know
- Use of enum in C programming
- The character set of C Language
- Format specifiers in C
- Data types in C