To initialize an array in C++, you can use array literals or the array
In this example,
numbers
with integer values using array literals.#include <iostream>
int main() {
int numbers[] = {1, 2, 3, 4, 5};
for (int i = 0; i < sizeof(numbers) / sizeof(numbers[0]); i++) {
std::cout << numbers[i] << " ";
}
return 0;
}
1 2 3 4 5
In this example,
fruits
with character values using the array
template.#include <iostream>
#include <array>
int main() {
std::array<char, 6> fruits = {'A', 'p', 'p', 'l', 'e'};
for (char fruit : fruits) {
std::cout << fruit << " ";
}
return 0;
}
A p p l e
In this tutorial, we learned How to Initialize an Array in C++ language with well detailed examples.