Arrays in C

In C programming language, an array is a collection of elements of the same data type that are stored in contiguous memory locations. Arrays are useful for storing and accessing large amounts of data efficiently.

To declare an array in C, you need to specify the data type of the elements and the number of elements in the array. Here’s an example of how to declare and initialize an array in C:

int arr[5] = {1, 2, 3, 4, 5};

In this example, we declare an integer array named “arr” that contains 5 elements, and initialize it with the values 1 through 5.

We can access the elements of an array using the array index. The index of the first element is 0, and the index of the last element is one less than the size of the array. Here’s an example of how to access the elements of an array:

int arr[5] = {1, 2, 3, 4, 5};

printf("%d\n", arr[0]); // prints 1
printf("%d\n", arr[1]); // prints 2
printf("%d\n", arr[2]); // prints 3

In this example, we declare an integer array named “arr” and initialize it with the values 1 through 5. We then use the array index to access the elements of the array.

We can also use a loop to iterate over the elements of an array. Here’s an example of how to use a loop to iterate over the elements of an array:

int arr[5] = {1, 2, 3, 4, 5};
int i;

for (i = 0; i < 5; i++) {
    printf("%d ", arr[i]);
}
printf("\n");

In this example, we declare an integer array named "arr" and initialize it with the values 1 through 5. We then use a for loop to iterate over the elements of the array and print them to the console.

Arrays in C have some limitations, such as a fixed size and the inability to resize once they are created. However, arrays are a powerful feature of C programming language that enable the programmer to store and access large amounts of data efficiently.