Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Arrays

C Programming Language Array

An array is a set/group of items having a similar data type stored in contiguous memory locations.

For the better understanding of the array, we will explain it by giving a simple example.
Suppose we want to create a record for 50 Students that contains a roll number of each student.
Since roll number is an integer value, we will need to use int variable to store roll number of each student.
Therefore we should use 50 int variables to store roll numbers of 50 Students, as shown below:

int main()
{
    int roll_no_1;
    int roll_no_2;
    int roll_no_3;
    ------------
    ------------
    int roll_no_50;
}

Declaring so many variables of the same type to store the values of same type & same purpose makes the program very complex and lengthy.
Handling more number of variables in program results in a big headache for the programmer.
To avoid this, C programming introduces a concept known as the array.
Array helps us to store multiple values of same data type using the only single variable.
Therefore in the above program with the help of array, we can store 50 different roll numbers using just one variable as shown below, instead of using 50 different int variables.

int main()
{
    int roll_no[100];
}

Now let’s learn Array in detail

As stated earlier, an array is a set/group of items having a similar data type arranged in a contiguous memory location.
Each item is known as an element of the array.
At a time array can contain elements of only one data type.
For example, if any array is declared with data type int, then it can contain elements of only int data type. If any array is declared with data type char, then it can contain elements of only char data type and so on.
This provides type safety to an array.
The number of elements that array can contain is equal to the size specified at the time of its declaration.
When we declare any array with specific type and size, memory locations with respect to that declared type & size are reserved in memory.
Memory locations are reserved in sequence.
Therefore all the array elements are present in contiguous memory locations.
The size of reserved memory depends on the type of the array and machine we use.
For example, on a 16-bit machine, size for every integer type is 2 bytes.
On a 32-bit machine, size for every integer type is 4 bytes.
Here we have considered 32-bit machine.
Therefore each integer array element consists of 4 bytes and all the array elements are stored in contiguous memory locations with the difference of 4 bytes.
On a 16-bit machine, if the size of integer array is 10 then 20 bytes (2 bytes for each element) are reserved in memory with the difference of 2 bytes as shown below:

C Programming Language Integer Array on 16 bit Machine

On a 32-bit machine, if the size of integer array is 10 then 40 bytes (4 bytes for each element) are reserved in memory with the difference of 4 bytes as shown below:

C Programming Language Integer Array on 32 bit Machine

Array elements can be accessed/obtained with the help of subscripts.
The value between [ ] (left & right bracket) is known as subscript.
In C programming, subscript starts from zero and ends with one less than size.

Types of Array

  • One-dimensional array
  • Two-dimensional array
  • Three-dimensional array, and so on.

One dimensional array has one subscript.

Array with two or more subscripts is known as a multidimensional array.

Two-dimensional arrays have two subscripts.

Three-dimensional arrays have three subscripts, and so on.

 

Leave a reply