How to swap two arrays without using temporary variable in C language?

In C programming, swapping two arrays without using a temporary variable can be achieved using arithmetic or bitwise operations. This technique exchanges the contents of two arrays by manipulating the values directly, avoiding the need for additional memory space.

Syntax

for(i = 0; i < size; i++) {
    array1[i] = array1[i] + array2[i];
    array2[i] = array1[i] - array2[i];
    array1[i] = array1[i] - array2[i];
}

Method 1: Using Arithmetic Operations

This method uses addition and subtraction to swap array elements −

#include <stdio.h>

int main() {
    int size = 5;
    int first[5] = {11, 12, 13, 14, 15};
    int sec[5] = {90, 80, 70, 60, 50};
    int i;
    
    printf("Before swapping:
"); printf("First array: "); for(i = 0; i < size; i++) { printf("%d ", first[i]); } printf("\nSecond array: "); for(i = 0; i < size; i++) { printf("%d ", sec[i]); } // Swapping arrays using arithmetic operations for(i = 0; i < size; i++) { first[i] = first[i] + sec[i]; sec[i] = first[i] - sec[i]; first[i] = first[i] - sec[i]; } printf("
\nAfter swapping:
"); printf("First array: "); for(i = 0; i < size; i++) { printf("%d ", first[i]); } printf("\nSecond array: "); for(i = 0; i < size; i++) { printf("%d ", sec[i]); } return 0; }
Before swapping:
First array: 11 12 13 14 15 
Second array: 90 80 70 60 50 

After swapping:
First array: 90 80 70 60 50 
Second array: 11 12 13 14 15 

Method 2: Using XOR Operation

The XOR bitwise operator can also be used to swap arrays without temporary variables −

#include <stdio.h>

int main() {
    int size = 4;
    int arr1[4] = {1, 2, 3, 4};
    int arr2[4] = {5, 6, 7, 8};
    int i;
    
    printf("Before XOR swapping:
"); printf("Array 1: "); for(i = 0; i < size; i++) { printf("%d ", arr1[i]); } printf("\nArray 2: "); for(i = 0; i < size; i++) { printf("%d ", arr2[i]); } // Swapping using XOR operation for(i = 0; i < size; i++) { arr1[i] = arr1[i] ^ arr2[i]; arr2[i] = arr1[i] ^ arr2[i]; arr1[i] = arr1[i] ^ arr2[i]; } printf("
\nAfter XOR swapping:
"); printf("Array 1: "); for(i = 0; i < size; i++) { printf("%d ", arr1[i]); } printf("\nArray 2: "); for(i = 0; i < size; i++) { printf("%d ", arr2[i]); } return 0; }
Before XOR swapping:
Array 1: 1 2 3 4 
Array 2: 5 6 7 8 

After XOR swapping:
Array 1: 5 6 7 8 
Array 2: 1 2 3 4 

Key Points

  • The arithmetic method works with all integer values but may cause integer overflow for large numbers.
  • The XOR method is safer as it avoids overflow issues and works with any data type of the same size.
  • Both methods have O(n) time complexity where n is the array size.
  • These techniques require O(1) extra space as no temporary variables are used.

Conclusion

Swapping arrays without temporary variables demonstrates efficient memory usage in C. The arithmetic approach is intuitive, while XOR operation provides a more robust solution for preventing overflow issues.

Updated on: 2026-03-15T13:50:49+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements