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

If you are learning “C programming“, it is very important to understand the difference between definition and declaration.

Now you are thinking about why important to know the difference between declaration and definition, then the reason behind is that if you don’t understand the difference, you’ll run into weird linker errors like “undefined symbol data” or “undefined reference to ‘data'”, where ‘data’ is an identifier.

In this blog post, you will learn the difference between declaration and definition. Here we are only discussing the differences but if you are just started learning the “C” and don’t have any knowledge, you can read the blog post “Learn declaration and definition“. It will help to understand the differences between declarations and definitions in C.

Comparison chart for declaration and definition:

 

Declaration Definition
Declaration introduces an identifier and describes its type. Definition is where the identifier is instantiated/implemented.
To allow references to any identifier, the compiler needs a declaration. For example,

int  main()
{
    printf("%d\n", count);

    return 0;
}

When you will compile the above code, you will get the compiler error “error: ‘count’ undeclared“. Because the compiler doesn’t know anything about ‘count‘.

The definition of an identifier is required of the linker to complete the linking process(to resolve the symbol resolution). For example,

int  main()
{
    extern int count;
    printf("%d\n", count);

    return 0;
}

When you will compile the above code you will get the linker error “undefined reference to `count’“. Because at the time of linking linker will not get the definition of ‘count’.

A declaration can not be used in the place of a definition. A definition can be used in the place of a declaration.
A variable or a function can be declared any number of times. For example,

int data;

int  main()
{

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

    printf("%d", data);
    return 0;
}

data was declared three times. You will not get any errors. Might your compiler throw a warning message!

A variable or a function can be defined only once.

int  main()
{
    int data;
    int data;
    int data;

    return 0;
}

You will get the linker error because you have defined ‘data’ more than once in the same scope. You are breaking the rule.

If you define a variable more than once, then the linker doesn’t know which of the definitions to link references to and complains about duplicated symbols.

Storage will not be allocated during the declaration. Storage will be allocated.
int fun(int);

extern int data;

The above is a declaration of variables and functions. This declaration is just for informing the compiler about the variable and function.

int fun(int val)
{

// some code
return val;
}

int data;

The above is a definition of variables and functions. Storage allocated here.

 

Recommended Articles for you:

Leave a Reply

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