C Programming Language


Array


The C language provides a capability that enables the user to design a set of similar data types, called array. An array is a collection of similar elements. These similar elements could be all ints, or all floats, or all chars, etc. Usually, the array of characters is called a ‘string’, whereas an array of ints or floats is called simply an array.

Instead of declaring individual variables, such as number0, number1, ..., and number100, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[100] to represent individual variables. A specific element in an array is accessed by an index.

 

Declaring Array :

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows:
Syntax :
type arrayName [ arraySize ];

This is called a single-dimensional array. The arraySize must be an integer constant greater than zero and type can be any valid C data type. For example, to declare a 10-element array called balance of type integer, use this statement:
int balance [5];

 

Intializing Array :

Initialize of an array in C Language as follows:

int balance[5] = {10,25,27,16,88}

The number of values between brackets { } cannot be larger than the number of elements which declare in the array between square brackets [ ].
int balance[] = {10,25,27,16,88}

Assigning single elements of array as per following example :
int no[5] = 20;

 

Accessing of Array :

With the help of for loop we can access the array as per follows :

Output :

Element [0] = 100
Element [1] = 101
Element [2] = 102
Element [3] = 103
Element [4] = 104
Element [5] = 105
Element [6] = 106
Element [7] = 107
Element [8] = 108
Element [9] = 109