Definition Vs Declaration Vs Initialisation in C

Definition, Declaration and Initialization:

On this page we will discuss about definition, declaration and initialization in C programming language. In certain languages, it can be difficult to tell the difference between the three ideas. It depends on the programming language we’re using and the object we wish to declare, define, or initialise. 

Definition Vs Declaration Vs Initialisation

About

Definition:

  • When declaring an identifier, we give the code processor instructions to allocate, or reserve, a sizable enough memory portion for it.
  • That implies and necessitates various things depending on the type of identifier.
  • The processor handles the function definition by converting it to machine code, inserting it in the reserved area, and associating the function name with the location of the code in memory.
  • The processor cannot determine how much room to reserve without the body.

Declaration:

  • A variable must have a starting value assigned in order to be initialized.
  • Initialization and definition may occur separately or concurrently in a statically typed language.
  • Initialization in computer programming is the process of giving a data object or variable its initial value.
  • The process of initialization is determined by the programming language, storage class, the type, etc., of the object that needs to be initialized, among other factors.

Initialization:

  • The C library’s realloc() function allows you to increase the size of memory blocks that have already been allocated.
  • Realloc is a function in C that allows you to increase the size of existing memory blocks without changing their content.
  • The realloc() function aids in shrinking memory that has previously been allocated using the malloc or calloc procedures.
  • The block’s contents are unaltered up to the smaller of the two sizes.

Example of Definition:

Run
#include<stdio.h>
#include<stdlib.h>
int main ()
{
  int n, i, *ptr, sum = 0;
  printf ("Enter number of elements = ");
  scanf ("%d", &n);
  ptr = (int *) malloc (n * sizeof (int));
  if (ptr == NULL)
    {
      printf ("Memory not allocated.");
      exit (0);
    }
  printf ("Enter elements = ");
  for (i = 0; i < n; ++i)
    {
      scanf ("%d", ptr + i);
      sum += *(ptr + i);
    }
  printf (" Total Sum = %d", sum);
  free (ptr);
  return 0;
}

Output:

Enter number of elements = 5
Enter elements = 9
8
7
6
5
Total Sum = 35

Example of Declaration:

Run
#include<stdio.h>
#include<conio.h>
int main ()
{
  int a, b, result;
  a = 200;
  b = 300;
  result = a + b;
  printf ("Sum of two numbers = %d \n", result);
  return 0;
}

Output:

Sum of two numbers = 500 

Example of Initialization:

Run
#include<stdio.h>
int main ()
{
  int a = 7, b = 8, result;
  result = a + b;
  printf ("Sum of two numbers is = %d \n", result);
  return 0;
}

Output:

Sum of two numbers is = 15 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription