looping through an array in C

To loop through an array in C, you can use a for loop. Here is an example of how you can do it:

int main() {
   int numbers[] = {1, 2, 3, 4, 5};
   int length = sizeof(numbers) / sizeof(numbers[0]);

   for (int i = 0; i < length; i++) {
      printf("%d ", numbers[i]);
   }

   return 0;
}

In this example, we have an array called numbers with 5 elements: 1, 2, 3, 4, and 5. The sizeof(numbers) gives the total number of bytes occupied by the array, and sizeof(numbers[0]) gives the number of bytes occupied by a single element of the array. By dividing the total number of bytes by the number of bytes per element, we can calculate the length of the array.

The for loop is used to iterate over the elements of the array. It starts with an initialization statement int i = 0, which sets the loop variable i to 0. The loop condition i < length specifies that the loop should continue as long as i is less than the length of the array. The loop variable is incremented by i++ after each iteration.

Inside the loop, we can access each element of the array using the index i. In this example, we print each element of the array using the printf function, followed by a space.

After the loop, the program returns 0, indicating successful execution.

This code will output: 1 2 3 4 5, which are the elements of the array.