In C, an array is a collection of elements of the same type, stored in contiguous memory locations. Each element in the array is identified by an index, starting from 0.
To declare an array in C, the following syntax is used:
type array_name[array_size];
For example, to declare an array of integers named “my_array” with a size of 5, the following syntax is used:
int my_array[5];
Arrays can be initialized at declaration time, like this:
int my_array[5] = {10, 20, 30, 40, 50};
Arrays can be accessed using the index operator, [], like this:
int x = my_array[2]; // assigns 30 to x
Arrays can be used in loops to iterate over the elements, like this:
for (int i = 0; i < 5; i++) { printf("%d ", my_array[i]); }
Multi-Dimensional Arrays
C also supports multi-dimensional arrays, which are arrays of arrays. The following syntax is used to declare a 2D array:
type array_name[rows][columns];
For example:
int matrix[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
In this example, the 2D array “matrix” has 3 rows and 3 columns.
Arrays are an important data structure in programming, and are used to store and manipulate large amounts of data efficiently. C supports a wide range of array types, including single-dimensional and multi-dimensional arrays.