Accessing array elements
To access array elements in C programming, you can use the index notation. The index of an array starts from 0, so the first element of the array is at index 0, the second element is at index 1, and so on.
Here's an example of accessing array elements in C:
#include <stdio.h> int main() { int numbers[5] = {10, 20, 30, 40, 50}; // Accessing array elements printf("The first element is: %d\n", numbers[0]); printf("The second element is: %d\n", numbers[1]); printf("The third element is: %d\n", numbers[2]); printf("The fourth element is: %d\n", numbers[3]); printf("The fifth element is: %d\n", numbers[4]); return 0; } |
Output:
The first element is: 10 The second element is: 20 The third element is: 30 The fourth element is: 40 The fifth element is: 50 |
In this example, we declare an integer array `numbers` with five elements. We then access each element using the index notation `numbers[index]`, where `index` is the position of the element we want to access. The accessed elements are then printed using `printf()`.