Arrays in C programming language

In C language, an array is defined as the collection of similar type of data items stored at contiguous memory locations.

Arrays are of two types :

  1.     One-dimensional arrays

  2.     Two-dimensional arrays

One Dimensional Arrays :

We can declare an one dimensional array in the c language in the following way.
data_type array_name[array_size];

Example :
int marks[99];

Here, int is the data_type, marks is the array_name, and 99 is the array_size.

Accessing Array Elements : An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. Arrays have 0 as the first index.

Example :
marks[0]=99;
marks[1]=95;
marks[2]=85;

Here marks are the array name with each index value. The above arrays can be initialize as follow :

int marks[3]={99,95,85};

In such case, there is no requirement to define the size. So it may also be written as the following code.
int marks[]={99,95,85};

Example :
#include <stdio.h>
int main()
{
      int marks[5];
      marks[0]=99;
      marks[1]=95; 
      marks[2]=85;
      marks[3]=80; 
      marks[4]=83;
      marks[5]=75; 
      printf("Marks is %d",marks[1]);
}

Output :
Marks is 95


Two Dimensional Arrays : The two-dimensional array can be defined as an array of arrays.

We can declare a two dimensional array in the c language in the following way.
data_type array_name[rows][columns];

Example :
int numbers[2][4];

Here, int is the data_type, numbers is the array_name, 2 is the number of rows, and 4 is the number of columns.

0 Comments