C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array after its declaration.

Why should we use an Array?

We should use an array because of its properties:

  • It reduces the code while using the array.
  • It is easy to traverse the data.
  • It is easy to short the element of an array.
  • It is easy to access the data randomly.

There are two types of arrays in C language:

  • 1-D Array
  • 2-D Array

One Dimensional Array:

A list of items can be given one variable name using only one subscript and such a variable is called one-dimensional array.

Declaration of C Array

We can declare an array by using the following syntax.

Initialization of Array

There are two ways to initialize the array:

  1. First way: We can initialize the array of elements inside the bracket with a comma in one line.
  1. Second way: We can initialize the array of the elements one by one. It is easy to access the elements in comparison to the first way.

Example

 

Let us consider an example of an array;

Output

Two Dimensional Array

Two-dimensional arrays are used in the form of row and columns. It is also known as matrices

The 2D, 3D or other dimension is also known as a multidimensional array.

Declaration of two-dimensional arrays

The given following syntax to declare the 2D:

Syntax:

Let us consider an example:

Initialization of 2D Array in C

Example:

Output

There are following some important points while using an array:

  • We must use the same data element or data at the time of initialization.
  • Array index will start with ‘0’ and end with ‘size-1’.

It uses static memory allocation at the time of working with an array.

Leave a Comment