Arrays in C Programming

Array in C programming language is a collection of fixed size data belongings to the same data type. An array is a data structure which can store a number of variables of same data type in sequence. These similar elements could be of type int, float, double, char etc.

For Example
int age[100]; /* Integer array of size 100 */
float salary[10]; /* floating point array of size 10 */

Suppose, you want to store salary of 100 employees. You can store salaries of all employees by creating 100 variable of int data type individually like employeeSalary1, employeeSalary2, employeeSalary3 .... employeeSalary100. This is very tedious, time consuming and difficult to maintain method of storing 100 salaries.

C programming language provides support for array data structure, which solves the problem of storing N similar data. We can declare an integer array of length 100 to store salaries of 100 employees

    int employeeSalary[100];

In the above array declaration, we declared an array of name "employeeSalary" which can store maximum of 100 integer data in contiguous memory locations. We can access the individual elements of an array using their index.

  • employeeSalary[0] refers to the first element of array.
  • employeeSalary[1] refers to the second element of array.
  • employeeSalary[99] refers to the last element of array which is 100th element.
All elements of array are stored in the contiguous memory locations.

Suppose, we have an integer array of size 7 whose name is score. The base address or starting address of array score is 1000(base address is same as address of first element score[0]) and the size of int be 2 bytes. Then, the address of second element(score[1]) will be 1002, address of third element(score[2]) will be 1004 and so on.

C Array Memory Map
Important Facts about Arrays in C
  • An array is a collection of variables of same data types.

  • All elements of array are stored in the contiguous memory locations.

  • The size of array must be a constant integral value.

  • Individual elements in an array can be accessed by the name of the array and an integer enclosed in square bracket called subscript/index variable like employeeSalary[5].

  • Array is a random access data structure. you can access any element of array in just one statement.

  • The first element in an array is at index 0, whereas the last element is at index (size_of_array - 1).
Advantage of Array in C
  • Less amount of code : Using array we can aggregate N variables of same data type in a single data structure. Otherwise we have to declare N individual variables.

  • Easy access of elements : We can access any element of array using array name and index. We can access all elements serially by iterating from index 0 to size-1 using a loop.

  • Easy to implement algorithms : Certain algorithms can be easily implemented using array like searching and sorting, finding maximum and minimum elements.

  • Random Access : We can access any elements of array in O(1) time complexity.

Declaration of Array in C


Like any other variable in C, we must declare an array first before using it. Below is the syntax of declaring a one-dimensional array.

data_type array_name[array_size];
  • array_name : It is a valid C identifier representing the name of the array.
  • array_size : It is the maximum number of elements which can be stored in array. It must be an integer constant greater than zero.
  • data_type : It represents the data type of the elements to be stored in array.
Examples of Array Declaration in C
  • An Array of 10 Integers
         int marks[10];
  • An Array of 50 Characters.
         char name[50];
  • Two Dimensional Array of Integers.
         int matrix[10][10];

Types of Array in C


There are two types of Arrays in C
  • One Dimensional Array
  • Multi Dimensional Array
One Dimensional Array in C
  • Single dimensional array is used to store data in sequential order.
  • Single dimensional array contains only one subscript/index in their declaration, like
        int marks[100];
  • In one dimensional array, we can access any element using one index reference, like marks[5].
Multi Dimensional Array in C
  • Array having more than one subscript variable/index is called multi dimensional array.
  • Multi dimensional array is array of array.
  • Multi dimensional array contains multiple subscript/index in their declaration, like
        int matrix[10][20];
  • In multi dimensional array, we can access any element using multiple index reference, like matrix[4][8].
Arrays in Details
Click on the links below to know more about arrays in C

Sample Array C Programs

C Program to read and print elements of an array
C Program to find sum of all elements of an array
C Program to find maximum elements in an array
C Program to find minimum element in an array
C program to insert an element in an array
C program to delete an element from an array
C program to reverse an array
C program to find second largest element in array
C program to delete duplicate elements from an array
C program to delete duplicate elements from a sorted array
C Program to print unique elements of an unsorted array