In this tutorial, we will learn about arrays in C. We will cover the basics of declaring and using arrays, including accessing and modifying elements.
An array is a collection of elements of the same type stored in contiguous memory locations. It allows you to store multiple items under a single name and access them using an index.
The syntax to declare an array in C is:
type arrayName[arraySize];
We will learn how to declare, initialize, access, and modify arrays in C.
arr
with 5 elements.#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
1 2 3 4 5
arr
with 5 elements.#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
printf("The third element is: %d\n", arr[2]);
return 0;
}
The third element is: 30
arr
with 5 elements.#include <stdio.h>
int main() {
int arr[5] = {5, 10, 15, 20, 25};
arr[1] = 50;
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
5 50 15 20 25