C Array Introduction : What is Array

An array is a linear data structure, which can hold a collection of variables belongings to the same data type.

In simple words an array is a special variable, which can hold more than one value at a time. Each value is an element of the array and has an associated index number. We can refer to a particular element in the array by using the name of the array and the index number,which starts from zero.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. For example the below diagram shows and array arr[].


Declaration of Array in C :

In C language array can be declared by following way :
datatype array_name[ArraySize];
Where :
  • datatype specifies the type of data elements of an array.
  • array_name is name of the array variable.
  • ArraySize specifies the number of elements on an array, it is also known as subscripts.
For example :
int myarr[10];
The above statement creates an integer array of 10 elements. At here also note that the index of an array starts from 0 to ArraySize - 1, means the first element of array will be myarr[0] and last element of array will be myarr[9] (because of 'ArraySize -1' or 10-1 = 9).

Initialization of An Array :

An array can be initialized at the time of declaration or one by one after declaration.
int myarr[5] = {10, 20, 30, 40, 50};
Where all the values are common separated. And if we initialize the array at the declaration time, then its not necessary to provide the ArraySize or subscript. For example :
int myarr[] = {10, 20, 30, 40, 50};
The above statement will also be valid. We can also initilized the array elements on by one. For example :
int myarr[5]; // declaration
 
myarr[0] = 10;
myarr[1] = 20;
myarr[2] = 30;
myarr[3] = 40;
myarr[4] = 50;

Accessing Array Values :

In array, an element can be accessed by  using the index with the name of array. This is done by placing the index of the element within square brackets after the name of the array. For example :
myarr[2];
The above statement gives the value of 3rd element or array myarr. Now lets see some example of array.

Example 1 :
#include <stdio.h>

int main() {
  int myarr[5] = {10, 20, 30, 40, 50};

  printf("1st element : %d\n", myarr[0]);
  printf("2nd element : %d\n", myarr[1]);
  printf("3rd element : %d\n", myarr[2]);
  printf("4th element : %d\n", myarr[3]);
  printf("5th element : %d\n", myarr[4]);

  return 0;
}
Output :

1st element : 10
2nd element : 20
3rd element : 30
4th element : 40
5th element : 50

Example 2 :
#include <stdio.h>

int main() {
  int myarr[5] = {10, 20, 30, 40, 50};

  // prints array values
  int x;
  for(x=0; x<5; x++) {
    printf("%d\n", myarr[x]);
  }
}
Output :

10
20
30
40
50

Example 3 :
#include <stdio.h>

int main() {
  int myarr[5];
 
  myarr[0] = 10;
  myarr[1] = 20;
  myarr[2] = 30;
  myarr[3] = 40;
  myarr[4] = 50;

  // prints array values
  int x;
  for(x=0; x<5; x++) {
    printf("%d\n", myarr[x]);
  }

  return 0;
}
Output :

10
20
30
40
50


Next Topic :