Difference between Declaration and Definition in C

Mostly beginner programmers are not aware of difference between declaration and definition in C. In this tutorial I will explain the both terms clearly.

Declaration

It tells compiler about the name and type of the identifier (variable, function, array, etc.). Memory is not reserved in this case.

Variable Declaration

extern int num;

In above example we declared a variable. It tells the compiler that its name is num and it is of integer type. Space is not allocated for it in memory.

Using extern keyword is must while declaring variable in C.

Function Declaration

int add(int, int);

In above example we declared a function. It tells the compiler that function name is add, it takes two arguments and returns a value of integer type.

Using extern keyword is optional while declaring function. If we don’t write extern keyword while declaring function, it is automatically appended before it.

When compiler encounters variable, function, class, etc. declaration then it understands that the definition is somewhere else in the program or any other file.

Definition

It tells compiler about the working of variable, function (body of function), etc. Memory is reserved in this case.

Variable Definition

int num = 10;      //declaration and definition both at a time

In above example we defined a variable and assigned some value to it. Here memory for variable will be allocated.

Function Definition

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

In this example we are defining the function i.e. how function add will work.

Declaration is really useful in case we defined a function in one file and used it in different files. All we have to do is declare the function in one line in whatever file we have used it.

Note: We can re-declare a variable, function, class, etc multiple times but can define it only once.

Difference between Declaration and Definition in C

S.No. Declaration Definition
1 Tells compiler about name and type of variable, class, function, etc. Tells compiler about what value stored in variable or working of function, class, etc.
2 Memory allocation is not done. Memory allocation is done.
3 Can re-declare multiple times. Can define only once.

Comment below if you have queries or found anything incorrect in above tutorial for declaration vs definition.

4 thoughts on “Difference between Declaration and Definition in C”

  1. void main() {

    int x;
    printf(“\n sizeof x = %d\n”, sizeof(x));

    }

    Output : sizeof x = 4

    So the declaration itself assigning memory right?

    1. Here int x; defines a variable of primitive type. The compiler implicitly initializes that with 0 (in contrast to global variables) whereas that value is derived from the primitive data type.

      Watch this:

      class Item; // declaration

      class List() // definition
      {
      private: items *Item; // compiled by the compiler, but cannot be linked by the linker

      public: void add() …
      }

Leave a Comment

Your email address will not be published. Required fields are marked *