CategoryC Program

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

  1. array[MAX_SIZE] creates space for up to 100 integers.
  2. The program reads size and checks that it is between 1 and MAX_SIZE.
  3. The first for loop stores the input values at indices 0 through size - 1.
  4. The second for loop starts with index = size - 1, which is the last valid array index.
  5. After printing an element, index-- moves to the previous position. The loop stops after printing the element at index 0.

For an array of five elements, the indices visited by the reverse loop are:

Loop iterationIndexPrinted value
1450
2340
3230
4120
5010

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 the n elements once.
  • Extra space complexity: O(1), because reverse traversal uses only one loop variable and does not create another array.

Common Mistakes

  • Starting at size instead of size - 1.
  • Using index > 0, which skips the element at index 0. The correct condition is index >= 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.