Array in C

Before you kick start to reading array, you should understand why array concept is comes? How it is requires in C?

Let's read following program:

#include<stdio.h>
int main()
{
 int r;
 r=1;
 r=2;
 printf("The value of r = %d",r);
 getch();
 return 0;
}

The output of above program would be:
The value of r = 2

So, you can understand that when the value 2 is assigned into r, the earlier value of r (i.e. 1) is lost. Thus the ordinary variables are capable of holding only one value at a time.
What we do if we would want to store more than one value at a time in a single variable?
Here, the comes the concept of array.

What is array?
  1. Array is a collection of similar elements.
  2. The first element in the array is numbered 0, so the last element is 1 less than the size of the array.
  3. An array is also known as a subscripted variable.
  4. Before using an array, its type and dimension must be declared.
  5. The array elements are always stored in contiguous memory locations. This is a very important features of array.
Thus, an array is collection of similar elements. These similar elements could be all ints, or all floats, or all chars.

Array of characters is called a string whereas an array of ints or floats is called simply an array.

Keep in mind that all elements of any array must be the same type. i.e. cannot have an array of 10 numbers, of which 5 are ints and 5 are floats.

Comments