Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
Reverse Array in C
Next article icon

Reverse Array in C

Last Updated : 20 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

Reversing an array means the swapping last element and first element, second last element and second element, and so on. In this article, we will learn how to reverse an array in C.

The simplest method to reverse an array in C program is by using two pointers: one starting at the beginning (left) and one at the end (right) of the string. Swap the elements at these pointers while moving them towards centre of the array until the pointers meet.

C
#include <stdio.h>  void rev(int arr[], int n) {      	// Two pointers     int l = 0, r = n - 1;     while (l < r) {                // Swap the elements         int temp = arr[l];         arr[l] = arr[r];         arr[r] = temp;                  // Move pointers towards middle         l++;         r--;     } } int main() {     int arr[] = {1, 2, 3, 4, 5};     int n = sizeof(arr) / sizeof(arr[0]);    	// Reverse array arr     rev(arr, n);      for (int i = 0; i < n; i++)         printf("%d ", arr[i]);     return 0; } 

Output
5 4 3 2 1 

Apart from the above method, an array can also be reversed using the methods shown below:

Table of Content

  • Using Recursion
  • Using a Temporary Array

Using Recursion

This method is the recursive implementation of two pointer approach but uses recursive call to swap the elements and move the two pointers.

C
#include <stdio.h>  void rev(int arr[], int l, int r) {     if (l >= r) {         return;     }          // Swap the elements     int temp = arr[l];     arr[l] = arr[r];     arr[r] = temp;          // Recursively call function to reverse the   	// remaining part     rev(arr, l + 1, r - 1); }  int main() {     int arr[] = {1, 2, 3, 4, 5};     int n = sizeof(arr) / sizeof(arr[0]);    	// Reverse the array arr     rev(arr, 0, n - 1);      for (int i = 0; i < n; i++)         printf("%d ", arr[i]);     return 0; } 

Output
5 4 3 2 1 

Using Temporary Array

Another method is to create a new array to store the reversed elements by copying the array from last to first, then copy the contents back into the original array.

C
#include <stdio.h>  void rev(int arr[], int n) {     int temp[n];      	// Store elements into temp in reverse order     for (int i = 0; i < n; i++) {         temp[i] = arr[n - 1 - i];      }          // Copy reversed array back to original array     for (int i = 0; i < n; i++) {         arr[i] = temp[i];     } }  int main() {     int arr[] = {1, 2, 3, 4, 5};     int n = sizeof(arr) / sizeof(arr[0]);    	// Reverse the array arr     rev(arr, n);        for (int i = 0; i < n; i++)         printf("%d ", arr[i]);     return 0; } 

Output
5 4 3 2 1 

Next Article
Reverse Array in C

K

kartik
Improve
Article Tags :
  • C Programs
  • C Language
  • c-array
  • C Strings Programs
  • C Array Programs
  • C Examples

Similar Reads

    Reverse String in C
    In C, reversing a string means rearranging the characters such that the last character becomes the first, the second-to-last character becomes the second, and so on. In this article, we will learn how to reverse string in C.The most straightforward method to reverse string is by using two pointers t
    3 min read
    How to Reverse a String in C?
    In C, a string is a sequence of characters terminated by a null character (\0). Reversing a string means changing the order of the characters such that the characters at the end of the string come at the start and vice versa. In this article, we will learn how to reverse a string in C. Example: Inpu
    2 min read
    C Program to Traverse an Array in Reverse
    Write a C program to traverse a given array in reverse order that contains N elements.ExamplesInput: arr[] = {2, -1, 5, 6, 0, -3}Output: -3 0 6 5 -1 2Input: arr[] = {4, 0, -2, -9, -7, 1}Output: 1 -7 -9 -2 0 4Different Ways to Traverse an Array in Reverse Order in CWe can traverse/print the array in
    2 min read
    Reverse Number Program in C
    The reverse of a number of means reversing the order of digits of a number. In this article, we will learn how to reverse the digits of a number in C language. Example:Input: 12354Output: 45321Explanation: The number 12354 when reversed gives 45321Input: 623Output: 326Explanation: The number 623 whe
    2 min read
    C Program to Reverse a Stack using Recursion
    Write a program to reverse a stack using recursion, without using any loop. Example:  Input: elements present in stack from top to bottom 1 2 3 4 Output: 4 3 2 1  Input: elements present in stack from top to bottom 1 2 3Output: 3 2 1 Recommended PracticeReverse a StackTry It!Reverse a stack using Re
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences