Declaration and definition (C/C++)

When we started learning the programming language, we faced two fancy words declaration and definition. In the beginning, most programmers avoid understanding these two words’ declaration and definition.

Initially, this two-term declaration looks very confusing. But after practicing the programming problems you are not only able to understand this concept able also able to understand the difference between them.

In this blog post, you will learn the concept of declaration and definition with the help of some programming examples.

So lets’ get started.

 

Declaration and Definition:

Let’s see the declartion and definition one by one with the help of example codes.

Declaration:

An identifier must be declared before it is used. Why I am saying this let’s understand it with an example code,

The following code is displaying the value of the ‘count’.

#include<stdio.h>


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

    return 0;
}

 

Now I am asking what will be the output of the code?

I am pretty sure,  you will say compiler error. But still, if you are confused and don’t know why you are getting compiler errors, you must read the compilation process.

At the time of compilation, your compiler must aware of the identifier. In the above code, the compiler does not have any information about the ‘count’ that is the reason it will give an “error: ‘count’ undeclared”.

Now you are thinking about what will be the solution to this problem, so I will say damn, it is so simple. You only need to declare your identifier. Let’s see the below code,

#include<stdio.h>

int  main()
{
    int count = 10;

    printf("%d\n", count);

    return 0;
}

Output: 10

 

Now, you can see your problem has been solved with the declaration. So now the next question is what the “declaration” is.

So let’s see what is the meaning of the declaration in C/C++.

Basically, Declaration introduces an identifier and describe its type ( type of variable, or function) to the compiler. It only provides sureness to the compiler at the compile time that identifier exists with the given type, so that compiler proceeds for further compilation without needing all detail of this variable.

In other words, you can say that a declaration is what the compiler needs to accept references to that identifier.

When we declare a variable, then we only give the information of the variable to the compiler, but there is no memory reserve for it. It is only a reference, through which we only assure the compiler that this variable exists somewhere in the code.

These are the example of declarations:

extern int data;

extern int fun(int, int);

// extern can be omitted for function declarations
double foo(int, double);

 

Definition:

A definition is where the identifier is instantiated/implemented. It provides the all information that require to generate the machine code. Basically, definition is what the linker needs at the time of linking process (Symbol Resolution and Relocation).

These are definitions corresponding to the above declarations:

int data;


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

double foo(int a, double b)
{
    return (a*b);
}

 

In C/C++, variables get the storage at the time of definition. Generally, definition and declaration occur at the same time and a definition can be used in the place of a declaration but vice versa is not applicable. Example of declaration and definition at the same time:

//definition and declaration 
//of integer variable
int value; 

//definition and declaration 
//of character variable
char cValue; 

//declaration and definition of integer 
//variable with initialization.
int test = 0;

 

Declaration Scope:

C identifier (variable) that appears in the program is visible (i.e., can be used) only within a region of program text called its scope. It means each identifier has its own scope.

There are many types of scope in C/C++ programming like block scope, global scope, function scope,..etc. I will not discuss it in detail. I only want to let you know that each name that is introduced by a declaration is valid within the scope where the declaration occurs and you can not use the same name more than once in same scope.

For example, we have used ‘count’ name twice in the main function, so we will get the error.

#include<stdio.h>

int  main()
{
    int count = 10;
    int count = 0; //error

    printf("%d\n", count);

    return 0;
}

 

But you can use the same in different scope, like the below example in which once ‘count’ is in main function(function scope) and another is outside the main function(global scope).

#include<stdio.h>

int count = 10;

int  main()
{
    int count = 0; //ok

    printf("%d\n", count);

    return 0;
}

 

If you will compile the code you will not get any compiler error. But such duplication of names can lead to programmer confusion and errors, and should be avoided.

 

✍ Note: In C, An identifier can be declared as often as you want but can define only once.

 

Recommended Articles for you:

Leave a Reply

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