Declaration vs. Definition in C Language

  • Declaration: Tells the compiler “this thing exists, and this is what it looks like”, but does not allocate storage space for it. It is a promise that this identifier (variable or function) will be defined elsewhere.

  • Definition: Tells the compiler “please create this thing”. It allocates memory for the variable or provides the function body (code) for the function. A definition is theimplementation.

1. Declaration and Definition of Variables

Variable Declaration

  • Uses the keyword <span>extern</span>.

  • Indicates that this variable isdefined in another source file orlater in this file.

  • Does not allocate memory.

// Variable declaration examples
extern int a; // Declares an integer variable a, defined elsewhere
extern double pi; // Declares a double variable pi, defined elsewhere

Variable Definition

  • The compiler willallocate memory for the variable.

  • A variable canonly be defined once (One Definition Rule), but can be declared multiple times.

// Variable definition examples
int a;          // Defines an integer variable a and allocates 4 bytes (typically) of memory
double pi = 3.14159; // Defines a double variable pi, allocates memory and initializes it to 3.14159
char c;         // Defines a character variable c, allocates 1 byte of memory

Comprehensive Example:

Assume there are two C source files:<span>file1.c</span> and <span>file2.c</span>.

file1.c

#include <stdio.h>
// Defines global variable global_var
// The compiler will allocate memory for global_var here
int global_var = 100;
// Defines function func
void func() {
    printf("Global var in file1: %d\n", global_var);
}

file2.c

#include <stdio.h>
// [Declaration] global_var. Tells the compiler: "There is an integer variable global_var defined elsewhere, feel free to use it."
// The compiler will not allocate new memory for global_var here; it will link to the one in file1.c.
extern int global_var;
// [Declaration] function func. This is called a "Function Prototype."
// Tells the compiler: "There is a function called func, it has no return value, no parameters, its definition is elsewhere."
void func(void);
int main() {
    // Can use global_var here because it has been declared
    printf("Accessing global_var from file2: %d\n", global_var);
    // Can call func
    func();
    // [Definition] a local variable local_var
    // The compiler will allocate memory for local_var on the stack
    int local_var = 50;
    printf("Local var in main: %d\n", local_var);
    return 0;
}

Compilation and Linking: Compile both files together:

gcc file1.c file2.c -o program
./program

Output:

Accessing global_var from file2: 100
Global var in file1: 100
Local var in main: 50

2. Declaration and Definition of Functions

This concept also applies to functions and is more common.

Function Declaration (Function Prototype)

  • Contains only the function’sreturn type, function name, and parameter list (parameter types).

  • Does not end with curly braces <span>{}</span>, but ends with a semicolon <span>;</span>.

  • Tells the compiler: “There is this function, its interface looks like this, now it can check if calls to it are correct (parameter types, count), its definition will be provided later.”

Function Definition

  • Contains the function’sreturn type, function name, parameter list (with parameter names), and function body (code within curly braces <span>{}</span>).

  • Provides the actual implementation code for the function.

Example:

#include <stdio.h>
// [Function Declaration] / [Function Prototype]
// Tells the compiler: There is a function called add, it takes two int parameters and returns an int.
// The compiler sees this and knows how to handle calls to add in the main function.
int add(int a, int b);
// [Function Declaration] / [Function Prototype]
void print_message(void);
int main() {
    int num1 = 5, num2 = 3;
    // Since add has already been declared, the compiler allows calling it here
    // The compiler will check if the number and types of parameters match
    int sum = add(num1, num2);
    printf("Sum: %d\n", sum);
    print_message();
    return 0;
}
// [Function Definition]
// Here is the specific implementation of the function add.
// The parameter list must include the parameter names (a and b) because they will be used in the function body.
int add(int a, int b) {
    return a + b;
}
// [Function Definition]
void print_message(void) {
    printf("Hello from print_message!\n");}

Why is Function Declaration Needed?C language is compiled top-down. When the compiler sees <span>add(num1, num2)</span> in the <span>main</span> function, it does not yet know what <span>add</span> is. If it is not declared beforehand, older versions of the compiler may throw an error or make incorrect assumptions (thinking it returns <span>int</span>), which can lead to serious runtime errors. Early declaration allows the compiler to performtype checking, which is an important aspect of C language safety.

Summary and Analogy

Feature Declaration Definition
Purpose Introduce the name and type of the identifier Create the entity, allocate memory or provide code
Memory Does not allocate storage space Allocates storage space (for variables) or provides instructions (for functions)
Times Can be declared multiple times Can only be defined once
Keyword Variables use <span>extern</span>, functions do not need Variables directly write type, functions need function body <span>{}</span>
Relationship Promises “I will exist” Fulfills the promise “I am here”

Analogy:

  • Declaration is like your resume. It tells the company: “I have a skill of a senior C language engineer (like <span>int add(int, int);</span>). The resume itself is not the engineer.

  • Definition is like you going to work at the company. You are the implementation of that skill. The company has prepared a workstation for you (allocating memory), and you start working (executing code).

A definition also serves as a declaration. For example, <span>int a;</span> both defines the variable <span>a</span> and declares it. But a declaration is never a definition.

Understanding this distinction allows for better organization of code (using header files <span>.h</span> for declarations, source files <span>.c</span> for definitions), and understanding compilation and linking errors.

Leave a Comment