Computer >> Computer tutorials >  >> Programming >> C programming

Arrays in C Language


Array is a collection of same type of elements at contiguous memory location. The lowest address corresponds to the first element while highest corresponds to last element.

Array index starts with zero(0) and ends with the size of array minus one(array size - 1). Array size must be integer greater than zero.

Let us see an example,

If array size = 10
First index of array = 0
Last index of array = array size - 1 = 10-1 = 9

Here is the syntax of arrays in C language,

type array_name[array_size ];

The following is how you can initialize an array.

type array_name[array_size] = { elements of array }

Here is an example of arrays in C language,

Example

#include <stdio.h>
int main () {
   int a[5];
   int i,j;
   for (i=0;i<5;i++) {
      a[i] = i+100;
   }
   for (j=0;j<5;j++) {
      printf("Element[%d] = %d\n", j, a[j] );
   }
   return 0;
}

Output

Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104