Declaration, definition and initialization of variable and function

Many times we come across these words in c for a variable or even for a function.

Declaration of a variables:

It simply means that the variable is somewhere present in the program but the memory is not allocated. It only refers to a variable name and what type of data the variable will hold

example: extern int a;
So extern keyword before a variable is simply a declaration of a variable.

Definition of a variable:

Once we define a variable, means a memory is allocated to that variable.

example: int a;
This memory may contain a garbage value.

Initialization of a variable:

When we assign a value to a variable, that is referred as initialization of a variable. The garbage value is over written by our new value.

example: int a = 10;
In fact above example is both defining and initializing a variable.

Function Declaration:

This is simply a prototype for a function that is it conveys following information:

  • It defines return type of a function.
  • Name of the function
  • Arguments for the function.
  • Order of arguments.

Function Definition:

It is the real logic or body of the function.

#include <stdio.h>

int add ( int num1, int num2); => Function declaration

int main()
   int num1 =10;   => Variable definition and assignment/initialization
   int num2 =20;
   int result = add (num1, num2);
   printf("\n Result is %d", result);
   return 0;
 int add ( int num1, int num2) ===> Function definition
   return (num1 + num2);

So function declaration tells the compiler that expect a function name “add” with return type int and number of argument is 2 and order is int , int
If we do not follow this prototype, compiler will throw error.

Categories: C Language

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: