Learning Sections show
Arrays in C
Example
#include<stdio.h> // Include the standard input-output library
int main() {
// Array declaration and initialization
int arr[5] = {1, 2, 3, 4, 5};
// Accessing and printing array elements
printf("Elements of the array:\n");
for (int i = 0; i < 5; i++) {
printf("%d\n", arr[i]);
}
return 0;
}
Explanation:
Arrays in C are used to store multiple values of the same type in a single variable. They provide a way to store and access data sequentially.
In this example:
#include<stdio.h>: Includes the standard input-output library which allows us to use theprintffunction.int main(): The main function where the program execution begins.int arr[5] = {1, 2, 3, 4, 5};: Declares an integer array namedarrwith 5 elements, and initializes it with values 1, 2, 3, 4, 5.printf("Elements of the array:\n");: Prints a header for the array elements.for (int i = 0; i < 5; i++): A loop that iterates through the array elements.printf("%d\n", arr[i]);: Prints each element of the array.return 0;: Ends the main function and returns 0 to the operating system, indicating that the program ended successfully.
In summary, arrays in C allow you to store a collection of values in a single variable, making it easier to manage and manipulate data. They are essential for tasks that require working with multiple related values.