Introduction to arrays in C language

In C, an array is a group of related data elements that are kept in close proximity and retrieved by indexing. The array’s index can be used to access any element within it.

Syntax of declaring array:

data_type array_name[array_size];

Here, array name is the name of the array, array size is the maximum number of elements the array can carry, and data type is the type of data that the array will hold (such as int, float, char, etc.).

When we need to store and manage a lot of the same sort of data, arrays come in handy. For instance, we can define an array of size 100 instead of 100 variables if we need to hold the grades of 100 students.

Example of declaring array:

int marks[5]; // declares an array of 5 integers

This creates a marks array, which has 5 integers. The index, which ranges from 0 to the array size minus one, is used to access each element in the array. For instance, we use index 0 to get the array’s first element:

marks[0] = 80; // sets the first element to 80

We can also initialize an array at the time of declaration, like this:

int marks[5] = {80, 75, 90, 85, 95}; // initializes the array with these values

In this example, we are initializing the marks array with 5 values.

Arrays can be of any data type, including int, float, char, etc. We can also have multi-dimensional arrays, which are arrays of arrays.