Day#43 'C' Coding Challenge

Day#43 'C' Coding Challenge

What is the difference between declaration and definition?

To explain the difference, let us consider below scenarios:

Scenario 1:
int sum(int,int,int);

Scenario 2:
int sum(int a,int b,int c)
{
    return a+b+c;
}

In the Scenario 1, a function is just named and it's associated arguments (int, int, int) and return type (int) are mentioned. This scenario is called definition. Definition here does not allocate any memory and it helps the compiler to understand that there is a function defined.

In the Scenario 2, a function is fully expanded and the actual functionality has been written. This scenario is called declaration. Declaration here allocates memory to the function. 

While the above two scenarios are about functions, let us consider below scenario:

Scenario 3:
int var = 10;

In the scenario, in the case of a variable, definition and declaration is the same. Compiler understands that variable has been declared and allocates memory at the same time.

Post a Comment

0 Comments