Initialization of Array in C

This “Initialization of Arrays in C language” page provides complete information about how to initialize array and default values of array if we don’t initialize.


 

Arrays Initialization:

  • Array variables can be declared either Globally or Locally.
  • We can initialize array directly using assignment operator.

 

Local variables declaration :
If we declare array of integer type and if we don’t initialize, all the variables implicitly initializes with “Garbage values”.

#include<stdio.h>
void main()
{
int arr[5] ;
}

 

 

If we declare array variable globally and if we don’t initialize, all the locations fill up with “Default values” depends on data type.

#include<stdio.h>
int arr[5];
void main()
{
..logic
}

 

If we initialize local or global array variable with only one element, remaining locations initializes with zeros…

#include<stdio.h>
int arr[5] = {10} ;
void main()
{
…logic
}

 

#include<stdio.h>
void main()
{
int arr[5] = {10} ;
…logic
}

 

We can initialize all the elements of array at a time as follows:

#include<stdio.h>
void main()
{
int arr[5] = {10,20,30,40,50} ;
…logic
}

Share this