In C++, you can declare an array using the array declaration syntax.
In this example,
numbers
of type int
and size 5
.#include <iostream>
using namespace std;
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
for(int i = 0; i < 5; i++) {
cout << numbers[i] << ' ';
}
return 0;
}
1 2 3 4 5
In this example,
names
of type string
and size 3
.#include <iostream>
#include <string>
using namespace std;
int main() {
string names[3] = {"Alice", "Bob", "Charlie"};
for(int i = 0; i < 3; i++) {
cout << names[i] << ' ';
}
return 0;
}
Alice Bob Charlie
In this tutorial, we learned How to Declare an Array in C++ language with well detailed examples.