C Program to print all elements of an array in reverse order
Learn how to print all elements of an array in reverse order in C without changing the original array, with examples and a step-by-step explanation.
An array stores its elements from index 0 to index size - 1. To print the elements in reverse order, we start at the last valid index and move backward until we reach index 0.
For example, if the array is:
10 20 30 40 50
then its elements in reverse order are:
50 40 30 20 10
This is called reverse traversal. It prints the elements from last to first but does not rearrange or modify the original array.
C Program to Print Array Elements in Reverse Order
#include <stdio.h>
#define MAX_SIZE 100
int main(void) {
int array[MAX_SIZE];
int size;
printf("Enter the number of elements: ");
if (scanf("%d", &size) != 1 || size < 1 || size > MAX_SIZE) {
printf("Please enter a size between 1 and %d.\n", MAX_SIZE);
return 1;
}
printf("Enter %d elements:\n", size);
for (int index = 0; index < size; index++) {
if (scanf("%d", &array[index]) != 1) {
printf("Invalid array element.\n");
return 1;
}
}
printf("Elements in reverse order: ");
for (int index = size - 1; index >= 0; index--) {
printf("%d ", array[index]);
}
printf("\n");
return 0;
}
Sample Output
Enter the number of elements: 5
Enter 5 elements:
10 20 30 40 50
Elements in reverse order: 50 40 30 20 10
The input may be entered on one line, as shown above, or on separate lines because scanf() treats spaces and line breaks in the same way when reading integers.
How the Program Works
array[MAX_SIZE]creates space for up to 100 integers.- The program reads
sizeand checks that it is between1andMAX_SIZE. - The first
forloop stores the input values at indices0throughsize - 1. - The second
forloop starts withindex = size - 1, which is the last valid array index. - After printing an element,
index--moves to the previous position. The loop stops after printing the element at index0.
For an array of five elements, the indices visited by the reverse loop are:
| Loop iteration | Index | Printed value |
|---|---|---|
| 1 | 4 | 50 |
| 2 | 3 | 40 |
| 3 | 2 | 30 |
| 4 | 1 | 20 |
| 5 | 0 | 10 |
The loop begins at size - 1, not size, because an array with size elements has valid indices from 0 to size - 1. Accessing array[size] would go beyond the array's valid range.
Using a Function to Print an Array in Reverse Order
We can put the reverse traversal in a function when the same operation is needed in more than one part of a program.
#include <stdio.h>
void printInReverse(const int array[], int size) {
for (int index = size - 1; index >= 0; index--) {
printf("%d ", array[index]);
}
printf("\n");
}
int main(void) {
int numbers[] = {7, -2, 15, 8};
int size = sizeof(numbers) / sizeof(numbers[0]);
printInReverse(numbers, size);
return 0;
}
Output
8 15 -2 7
The const keyword tells the compiler that printInReverse() only reads the array. The function cannot use its parameter to change the elements.
Does This Program Reverse the Array?
No. It only visits and prints the elements in reverse order. After the loop finishes, the elements are still stored in their original positions.
To actually reverse an array, the program must swap the first element with the last element, the second element with the second-last element, and so on. Reverse printing is useful when only the output order needs to change.
Time and Space Complexity
- Time complexity:
O(n), because the program prints each of thenelements once. - Extra space complexity:
O(1), because reverse traversal uses only one loop variable and does not create another array.
Common Mistakes
- Starting at
sizeinstead ofsize - 1. - Using
index > 0, which skips the element at index0. The correct condition isindex >= 0. - Using an unsigned loop variable for the reverse loop. An unsigned value cannot become negative, which can cause the loop to continue unexpectedly after it reaches zero.
By starting at the last valid index and decreasing the index after every iteration, we can print an array in reverse order with one simple loop and without changing the array itself.