c
C Programming Tutorial : Introduction

Arrays in C Language - With Examples:

An array is a collection of similar type of data items. Each data item is called an element of the array. The data type of the elements may be any valid data type like char, int, or float. The elements of array share the same variable name, but each element has a different index number known as a subscript.

The variables that we have used till now, can store only one value at a time. Consider a situation when we want to store and display the age of 100 employees. For this, we must do the following-

  1. Declare 100 different variables to store the age of employees.
  2. Assign a value to each variable.
  3. Display the value of each variable.

Although we can perform our task by the above three steps. Just imagine how difficult it would be to handle so many variables in the program. The program would become very lengthy. The concept of the arrays is useful in these types of situations where we can group similar types of data items.

For the above problem, we can take an array variable age [ 100] of type int. The size of this array variable is 100 so it can store 100 integer values.

The individual elements of this array are-

Syntax-

age[0], age[1], age[2], age[3],age[4], ………………………………….. age[98], age[99] 

In C the subscripts start from zero, so age[0] is the first element, age[ 1] is the second element of an array, and so on.

In C Programing language can be as two type Arrays.

  1. One dimensional
  2. Two Dimensional Array
  3. Multidimensional.

Advantages of an Array in C

  1. Random access of elements using array index.
  2. Use of less line of code as it creates a single array of multiple elements.
  3. Easy access to all the elements.
  4. Traversal through the array becomes easy using a single loop.
  5. Sorting becomes easy as it can be accomplished by writing less line of code.

Disadvantages of an Array in C

  1. Allows a fixed number of elements to be entered which is decided at the time of declaration. Unlike a linked list, an array in C is not dynamic.
  2. Insertion and deletion of elements can be costly since the elements are needed to be managed in accordance with the new memory allocation.