Array in C

Array is a collection of variables belongings to the same data type. You can store group of data of same data type in an array.

  • Array might be belonging to any of the data types
  • Array size must be a constant value.
  • Always, Contiguous (adjacent) memory locations are used to store array elements in memory.
  • It is a best practice to initialize an array to zero or null while declaring, if we do not assign any values to array.

 EXAMPLE FOR ARRAYS:

  • int a[10];       // integer array
  • char b[10];    // character array   i.e. string

 DECLARING AN ARRAY:

Like any other variable, arrays must be declared before they are used. General form of array declaration is:-

data-type variable-name[size];

int arr[10];  //Array Declaration

Here int is the data type, arr is the name of the array and 10 is the size of array. It means array arr can only contain 10 elements of int type.

Index of an array starts from 0 to size-1 i.e first element of arr array will be stored at arr[0]address and the last element will occupy arr[9].

INITIALIZATION OF AN ARRAY:

After an array is declared it must be initialized. Otherwise, it will contain garbage value(any random value). An array can be initialized at either compile time or at runtime.

COMPILE TIME ARRAY INITIALIZATION:

Compile time initialization of array elements is same as ordinary variable initialization. The general form of initialization of array is:-

data-type array-name[size] = { list of values };

int knp[4]={13,35,87,21}  // Integer Array Initialization
float lko[4] = {40.5,80.5,75.3};  //Float Array Initialization
int ald[4] = {54,86,56,45,32};  //Compiler Time Error

One important thing to remember is that when you will give moreinitialization(array elements) than the declared array size thanthe compiler will give an error.

#include<stdio.h>
void main()
{
  int i;
  int arr[] = {2,6,1};   //Compile Time Array Initialization
  for(i=0;i<3;i++)
  {
    print("%d\t", arr[i]);
  }
}

RUNTIME ARRAY INITIALIZATION:

An array can also be initialized at runtime using scanf() function. This approach is usually used for initializing large arrays, or to initialize arrays with user specified values. Example,

#include<stdio.h>
void main()
{
 int arr[4];
 int i,j;
 printf("Enter Array Elements");
 for(i=0;i<4;i++)
 {
   scanf("%d", arr[i]); //Run Time Array Initialization
 }
 for(j=0;j<4;j++)
  {
  print("%d\n", arr[j]);
  }
}

TYPES OF C ARRAYS:

There are 2 types of C arrays. They are,

  1. One dimensional array
  2. Multi dimensional array
    • Two dimensional array
    • Three dimensional array
    • four dimensional array etc…
ONE DIMENSIONAL ARRAY IN C:

Syntax : data-type arr_name[array_size];

Array declaration, initialization and accessingExample
Array declaration syntax:
data_type arr_name [arr_size];

Array initialization syntax:
data_type arr_name [arr_size]=(value1, value2, value3,….);

Array accessing syntax:
arr_name[index];
Integer array example:int age [5];
int age[5]={0, 1, 2, 3, 4};age[0]; /*0 is accessed*/
age[1]; /*1 is accessed*/
age[2]; /*2 is accessed*/

Character array example:char str[10];
char str[10]={‘H’,‘a’,‘i’};
(or)
char str[0] = ‘H’;
char str[1] = ‘a’;
char str[2] = ‘i;
str[0]; /*H is accessed*/
str[1]; /*a is accessed*/
str[2]; /*i is accessed*/

EXAMPLE PROGRAM FOR ONE DIMENSIONAL ARRAY IN C:

#include<stdio.h>
int main()
{
int i;
int arr[5] = {10,20,30,40,50};
// declaring and Initializing array in C
//To initialize all array elements to 0, use int arr[5]={0};
/* Above array can be initialized as below also
arr[0] = 10; 
arr[1] = 20; 
arr[2] = 30;
arr[3] = 40;
arr[4] = 50; */
for (i=0;i<5;i++)
 {
  // Accessing each variable
  printf("value of arr[%d] is %d \n", i, arr[i]);
 }
}
OUTPUT:
value of arr[0] is 10
value of arr[1] is 20
value of arr[2] is 30
value of arr[3] is 40
value of arr[4] is 50

TWO DIMENSIONAL ARRAY IN C:

  • Two dimensional array is nothing but array of array.
  • syntax : data_type array_name[num_of_rows][num_of_column];
Array declaration, initialization and accessingExample
Array declaration syntax:
data_type arr_name [num_of_rows][num_of_column];

Array initialization syntax:
data_type arr_name[2][2] = {{0,0},{0,1},{1,0},{1,1}};

Array accessing syntax:
arr_name[index];
Integer array example:int arr[2][2];
int arr[2][2] = {1,2, 3, 4};arr [0] [0] = 1;
arr [0] ]1] = 2;
arr [1][0]  = 3;
arr [1] [1] = 4;

EXAMPLE PROGRAM FOR TWO DIMENSIONAL ARRAY IN C:

#include<stdio.h>
int main()
{
int i,j;
// declaring and Initializing array
int arr[2][2] = {10,20,30,40};
/* Above array can be initialized as below also
arr[0][0] = 10; // Initializing array
arr[0][1] = 20;
arr[1][0] = 30;
arr[1][1] = 40; */
for (i=0;i<2;i++)
{
for (j=0;j<2;j++)
{
// Accessing variables
printf("value of arr[%d] [%d] : %d\n",i,j,arr[i][j]);
}
}
}

OUTPUT:
value of arr[0] [0] is 10
value of arr[0] [1] is 20
value of arr[1] [0] is 30
value of arr[1] [1] is 40

Follow Us On