• Home
• What is Array?

# What is Array?

An array is a collection of elements of the same type, stored in contiguous memory locations. In C language, an array is a way to store a fixed-size sequential collection of elements of the same type.

An array is declared by specifying the type of the elements, followed by the name of the array, and the size of the array enclosed in square brackets.

Here is an example of declaring an array of integers in C:

``int array; // declare an array of 10 integers``

In this example, the array is declared with a size of 10, and the type of the elements is int. The elements of the array are uninitialized, and have undefined values.

You can also declare and initialize an array with values:

``int array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // declare and initialize an array of 10 integers``

In this example, the array is declared and initialized with the given values, in the order they appear. The size of the array is inferred from the number of values provided.

You can access the elements of the array using the array subscript notation, as shown in the following example:

``````int array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // declare and initialize an array of 10 integers

printf("%d\n", array); // print the first element of the array
printf("%d\n", array); // print the last element of the array``````

You can also modify the elements of the array using the array subscript notation:

``````int array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // declare and initialize an array of 10 integers

array = 100; // modify the first element of the array
array = 200; // modify the last element of the array``````

You can also iterate through the elements of the array using a loop:

``````int array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // declare and initialize an array of 10 integers

int i;
for (i = 0; i < 10; i++) {
printf("%d ", array[i]); // print the element at index i
}
printf("\n");``````

In this example, the loop iterates from 0 to 9, and prints out the elements of the array. The output of this code is “0 1 2 3 4 5 6 7 8 9”.

You can use the sizeof operator to determine the size of the array:

``````int array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // declare and initialize an array of 10 integers

int size = sizeof(array) / sizeof(array); // calculate the size of the array
printf("The size of the array is %d\n", size);``````

In this example, the size of the array is calculated by dividing the total size of the array (in bytes) by the size of a single element (in bytes). The result is the number of elements in the array. The output of this code is “The size of the array is 10”.