In this C++ tutorial, you will learn how to print the elements of an array using a While loop, a For loop, and a range-based For loop. You will also learn how to print a C++ array in one line, format the output with commas and brackets, get the array length safely, and avoid printing the array address instead of its elements.
C++ Print Array Elements
For a built-in C++ array such as int arr[], print each element by visiting the array one element at a time. Use an index-based loop when you need the element position, or a range-based For loop when you only need the values.
for (int i = 0; i < array_length; i++) {
cout << arr[i] << " ";
}
The expression arr[i] accesses the element at index i. C++ array indexes start at 0, so an array with seven elements has valid indexes from 0 through 6.
1. Print array using While loop
In this example, we will use C++ While Loop to print array elements.
C++ Program
#include <iostream>
using namespace std;
int main() {
int arr[7] = {25, 63, 74, 69, 81, 65, 68};
int i=0;
while (i < 7) {
cout << arr[i] << " ";
i++;
}
}
Output
25 63 74 69 81 65 68
The loop starts with i = 0. On each iteration, arr[i] is printed and i is incremented. The condition i < 7 stops the loop before it goes beyond the last valid index.
2. Print array using For loop
In this example, we will use C++ For Loop to print array elements.
C++ Program
#include <iostream>
using namespace std;
int main() {
int arr[7] = {25, 63, 74, 69, 81, 65, 68};
for (int i=0; i < 7; i++) {
cout << arr[i] << " ";
}
}
Output
25 63 74 69 81 65 68
A For loop is useful when you need both the array element and its index. The initialization, condition, and increment are all written in the loop header.
3. Print array using ForEach statement
In this example, we will use C++ Foreach statement to print array elements.
C++ Program
#include <iostream>
using namespace std;
int main() {
int arr[7] = {25, 63, 74, 69, 81, 65, 68};
for (int element: arr) {
cout << element << " ";
}
}
Output
25 63 74 69 81 65 68
This form is called a range-based For loop in C++. It visits every element directly, so you do not need to manage an index or write the array length explicitly.
Print a C++ Array in One Line
To print all array values on one line, print a separator such as a space after each element, then print a newline after the loop.
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40, 50};
for (int value : arr) {
cout << value << " ";
}
cout << '\n';
}
Output
10 20 30 40 50
Print a C++ Array with Commas and Brackets
If the required output is [10, 20, 30, 40], print the separator only before elements after the first one. This avoids an extra comma at the end.
#include <iostream>
#include <iterator>
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40};
const size_t n = size(arr);
cout << "[";
for (size_t i = 0; i < n; ++i) {
if (i > 0) {
cout << ", ";
}
cout << arr[i];
}
cout << "]\n";
}
Output
[10, 20, 30, 40]
Get C++ Array Length Before Printing
Hard-coding the number of elements works for a fixed example, but it can become incorrect when the array changes. For a built-in array that is still an array in the current scope, C++17 provides std::size().
#include <iostream>
#include <iterator>
using namespace std;
int main() {
int arr[] = {25, 63, 74, 69, 81, 65, 68};
for (size_t i = 0; i < size(arr); ++i) {
cout << arr[i] << " ";
}
}
Before C++17, a common expression for the number of elements is sizeof(arr) / sizeof(arr[0]). This works only while arr is an actual array in that scope. It does not give the element count after the array has decayed to a pointer.
size_t length = sizeof(arr) / sizeof(arr[0]);
Print a C++ Array from a Function Without Losing Its Size
When a built-in array is passed to a function as a parameter such as int arr[], the parameter is treated as a pointer and its element count is not retained. One way to preserve the size is to accept the array by reference with a template.
#include <iostream>
#include <cstddef>
using namespace std;
template <size_t N>
void printArray(const int (&arr)[N]) {
for (size_t i = 0; i < N; ++i) {
cout << arr[i] << (i + 1 == N ? '\n' : ' ');
}
}
int main() {
int arr[] = {5, 10, 15, 20};
printArray(arr);
}
Output
5 10 15 20
Print a C++ Array Without Writing an Explicit Loop
A built-in numeric array does not have a general cout << arr operation that prints every element. If you do not want to write the loop yourself, you can use a standard algorithm such as std::copy() with an output iterator. The library still processes the elements one by one internally.
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40};
copy(begin(arr), end(arr), ostream_iterator<int>(cout, " "));
cout << '\n';
}
Output
10 20 30 40
Why cout << arr Does Not Print an Integer Array
For an integer array, the array name normally decays to a pointer to its first element. Sending that pointer directly to cout displays a pointer value rather than iterating through the array elements.
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 30};
cout << arr << '\n';
}
The exact pointer value is not the array contents. Character arrays are a special case: a null-terminated char array can be printed as a C-style string by the stream output operator.
Common C++ Array Printing Mistakes
- Going past the last index: use
i < length, noti <= length. - Hard-coding a stale length: prefer a range-based For loop or
std::size(arr)when possible. - Printing the numeric array name directly:
cout << arrprints a pointer value instead of every integer element. - Using
sizeofafter array-to-pointer decay: calculate the length before decay, pass the size separately, or preserve it with an array-reference template. - Leaving an unwanted trailing separator: print commas or other separators between elements instead of blindly appending one after every element.
Choosing the Right C++ Array Printing Method
Use a range-based For loop when you only need to print each value. Use an index-based For or While loop when the element position matters. For formatted output such as commas and brackets, use an index or track whether the current value is the first element. In reusable functions, make sure the array length is preserved or passed explicitly instead of assuming it can always be recovered from a pointer.
In this C++ Tutorial, we learned how to print array elements using looping statements.
C++ Print Array Editorial QA Checklist
- Verify each index-based example stops before the array length and never accesses an out-of-range element.
- Check that range-based For loop examples do not unnecessarily hard-code the array length.
- Confirm the
std::size()example is identified as C++17 or later. - Verify that examples distinguishing arrays from pointers do not use
sizeof(pointer)as an element count. - Check that comma-separated output has no extra comma before the closing bracket.
- Confirm the article does not imply that
cout << arrprints every numeric element, while retaining the separate behavior of null-terminated character arrays. - Ensure all new C++ snippets use
language-cpp, syntax-only snippets also usesyntax, and result-only blocks useoutput.
TutorialKart.com