In this C++ tutorial, you will learn how to initialise an array with values of a specific datatype, and go through some examples of how to initialize arrays of different datatypes.
C++ Initialize Array
To initialize a C++ Array, assign the list of elements separated by comma and enclosed in flower braces, to the array variable. Initialization can be done during declaration itself or later in a separate statement.
C++ Array Initialization during Declaration
Method 1
We can assign the list of elements to the array variable during declaration. If we do not specify the size of array, the number of elements we are initializing the array with, becomes the size of array.
In the following example, we have not specified any size in the square brackets.
int a[] = {7, 3, 8, 7, 2};
But, as we have assigned a list of five elements to the array, the size of the array becomes 5
.
Method 2
We can also specify the size of array, and assign a list of elements to the array. The number of elements in the list could be less than the size specified. Then, the compiler creates array with specified size, and assigns the elements one by one from the starting of array.
int a[10] = {7, 3, 8, 7, 2, 9, 5};
We have specified the array size to be 10
. But we have given only seven elements in the initializing list. So, the first seven elements of the array get initialized with the given list of elements, and the rest of array elements are initialized to zero.
Method 3
We can also declare an array with a specific size and initialize later, one element at a time, using index.
int a[10];
a[0] = 7;
a[1] = 3;
a[2] = 8;
a[3] = 2;
a[4] = 9;
C++ Initialize Array of Integers
In this example, we initialize two arrays of integers: one array with size not specified during declaration and the second with size specified and fewer elements initialized.
C++ Program
#include <iostream>
using namespace std;
int main() {
//initialize array with no size specified
int arr1[] = {7, 3, 8, 7, 2};
//initialize array with size specified, but with fewer elements
int arr2[10] = {52, 78, 18, 92, 96, 69, 84};
//print arrays
for (int i=0; i < sizeof(arr1)/sizeof(*arr1); i++) {
cout << arr1[i] << " ";
}
cout << endl;
for (int i=0; i < sizeof(arr2)/sizeof(*arr2); i++) {
cout << arr2[i] << " ";
}
}
Output
7 3 8 7 2
52 78 18 92 96 69 84 0 0 0
C++ Initialize Array of Strings
In this example, we initialize two arrays of strings: one array with size not specified during declaration and the second with size specified and fewer elements initialized.
C++ Program
#include <iostream>
using namespace std;
int main() {
//initialize array with no size specified
string arr1[] = {"hello" , "hi", "good" , "morning"};
//initialize array with size specified, but with fewer elements
string arr2[10] = {"hello" , "hi", "good" , "morning", "thank", "you", "user"};
//print arrays
for (int i=0; i < sizeof(arr1)/sizeof(*arr1); i++) {
cout << arr1[i] << " ";
}
cout << endl;
for (int i=0; i < sizeof(arr2)/sizeof(*arr2); i++) {
cout << arr2[i] << " ";
}
}
Output
hello hi good morning
hello hi good morning thank you user
C++ Initialize Array of Chars
In this example, we initialize two arrays of chars: one array with size not specified during declaration and the second with size specified and fewer elements initialized.
C++ Program
#include <iostream>
using namespace std;
int main() {
//initialize array with no size specified
char arr1[] = {'a', 'e', 'i', 'o', 'u'};
//initialize array with size specified, but with fewer elements
char arr2[10] = {'t', 'u', 't', 'o', 'r', 'i', 'a', 'l'};
//print arrays
for (int i=0; i < sizeof(arr1)/sizeof(*arr1); i++) {
cout << arr1[i] << " ";
}
cout << endl;
for (int i=0; i < sizeof(arr2)/sizeof(*arr2); i++) {
cout << arr2[i] << " ";
}
}
Output
a e i o u
t u t o r i a l
C++ Initialize Array of Floating Point Numbers
In this example, we initialize two arrays of floating point numbers: one array with size not specified during declaration and the second with size specified and fewer elements initialized.
C++ Program
#include <iostream>
using namespace std;
int main() {
//initialize array with no size specified
float arr1[] = {7.98652, 5.7443, 8.88888, 4.1255557, 2.22222};
//initialize array with size specified, but with fewer elements
float arr2[10] = {5.882, 5.578, 1.448, 92.232, .74965, .6669, 888.884};
//print arrays
for (int i=0; i < sizeof(arr1)/sizeof(*arr1); i++) {
cout << arr1[i] << " ";
}
cout << endl;
for (int i=0; i < sizeof(arr2)/sizeof(*arr2); i++) {
cout << arr2[i] << " ";
}
}
Output
7.98652 5.7443 8.88888 4.12556 2.22222
5.882 5.578 1.448 92.232 0.74965 0.6669 888.884 0 0 0
C++ Initialize Array of Long
In this example, we initialize two arrays of long numbers: one array with size not specified during declaration and the second with size specified and fewer elements initialized.
C++ Program
#include <iostream>
using namespace std;
int main() {
//initialize array with no size specified
long arr1[] = {798652, 57443, 888888, 41255557, 222222};
//initialize array with size specified, but with fewer elements
long arr2[10] = {5882, 5578, 1448, 92232, 74965, 6669, 888884};
//print arrays
for (int i=0; i < sizeof(arr1)/sizeof(*arr1); i++) {
cout << arr1[i] << " ";
}
cout << endl;
for (int i=0; i < sizeof(arr2)/sizeof(*arr2); i++) {
cout << arr2[i] << " ";
}
}
Output
798652 57443 888888 41255557 222222
5882 5578 1448 92232 74965 6669 888884 0 0 0
C++ Initialize Array of Booleans
In this example, we initialize two arrays of boolean values: one array with size not specified during declaration and the second with size specified and fewer elements initialized.
C++ Program
#include <iostream>
using namespace std;
int main() {
//initialize array with no size specified
bool arr1[] = {true, true, false, true, false};
//initialize array with size specified, but with fewer elements
bool arr2[10] = {true, false, false, false, false, true, true};
//print arrays
for (int i=0; i < sizeof(arr1)/sizeof(*arr1); i++) {
cout << arr1[i] << " ";
}
cout << endl;
for (int i=0; i < sizeof(arr2)/sizeof(*arr2); i++) {
cout << arr2[i] << " ";
}
}
Output
1 1 0 1 0
1 0 0 0 0 1 1 0 0 0
Conclusion
In this C++ Tutorial, we learned how to initialize an array in C++ in different ways, and for different datatypes, with example C++ programs.