Difference between Definition and Declaration (Declare vs Define in C)

In C programming, the terms declaration and definition are closely related, but they have different meanings.

A declaration introduces an identifier (variable or function) to the compiler, while a definition actually creates that identifier in memory or provides its complete implementation.

Understanding the difference between declaration and definition is very important for writing modular and error-free C programs.

 

What is Declaration in C?

A declaration tells the compiler:

  • The name of an identifier (variable or function names).
  • Its data type.
  • And that the identifier exists somewhere in the program.

The compiler uses this information for type checking and compilation.

Note: A declaration may or may not allocate storage.

 

Syntax of Declaration:

//for variable
extern data_type variable_name;


//For function
return_type function_name(parameter_list);

Example of Declaration:

extern int data;

extern int add(int, int);

int fun(int, char);

Explanation:

Let’s understand the meaning of variable and function declarations.

Variable Declaration:

extern int data;

  • Declares a variable named data.
  • Tells the compiler that the variable exists somewhere else.
  • Does not allocate storage

Function Declaration:

extern int add(int, int);

or

int fun(int, char);

  • Declares functions.
  • Specifies return type and parameters.
  • Does not provide the function body

In C, extern is optional for function declarations

 

What is Definition in C?

A definition actually creates the variable or function.

  • For variables, it allocates storage.
  • For functions, it provides the complete function body.

Syntax of Definition:

//variable Definition
data_type variable_name;

OR

//function Definition
return_type function_name(parameters)
{
    // function body
}

 

Example of Definition:

int data;

int add(int a, int b)
{
    return a + b;
}

Variable Definition:

int data;

  • Defines the variable data.
  • Allocates memory for the variable.

Function Definition:

int add(int a, int b)
{
    return a + b;
}
  • Defines the function add.
  • Provides the actual implementation of the function
Note: Every definition is also a declaration.

 

Explanation:

Let’s understand the meaning of variable and function definition.

Variable Definition:

int data;

  • Defines the variable data.
  • Allocates memory for the variable

Function Definition:

int add(int a, int b)
{
return a + b;
}
  • Defines the function add.
  • Provides the actual implementation of the function

 

Comparison chart for declaration and definition:

Declaration Definition
A declaration introduces an identifier and specifies its type. A definition is a declaration that actually creates the object or provides the function body.
The compiler requires a declaration before an identifier can be used.

int main(void)
{
    printf("%d\n", count);
    return 0;
}

The compiler reports an error because count has not been declared.

The linker requires a definition to resolve external references.

extern int count;

int main(void)
{
    printf("%d\n", count);
    return 0;
}

The code compiles, but linking fails if no definition of count exists in the program.

A declaration may appear multiple times as long as the declarations are compatible.

extern int data;
extern int data;
extern int data;

This is valid in C.

An identifier with external linkage should generally have only one definition in the entire program.

int data;
int data;

Multiple tentative definitions may be accepted by the compiler and treated as a single definition according to the C standard.

Some declarations are not definitions.

extern int data;
int fun(int);

These declarations only describe the identifier.

Every definition is also a declaration.

int data;

int fun(int val)
{
    return val;
}

Here, storage is allocated for data, and the function body defines fun.

A declaration alone does not necessarily allocate storage. A definition of an object typically allocates storage.

 

Recommended Articles for you: