You are here

C program to swap two numbers

C program to swap two numbers with and without using third variable, using pointers, functions (Call by reference) and using bit-wise XOR operator. Swapping means interchanging. For example, if in your C program you have taken two variables a and b where a = 4 and b = 5, then before swapping a = 4, b = 5 after swapping a = 5, b = 4. In the first C program to swap numbers we will use a temporary variable to swap two numbers.

Swapping of two numbers in C

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   int x, y, t;
  6.  
  7.   printf("Enter two integers\n");
  8.   scanf("%d%d", &x, &y);
  9.  
  10.   printf("Before Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);
  11.  
  12.   t = x;
  13.   x = y;
  14.   y = t;
  15.  
  16.   printf("After Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);
  17.  
  18.   return 0;
  19. }

Download Swap numbers program.

Output of program:
Enter two integers
23
45
Before Swapping
First integer = 23
Second integer = 45
After Swapping
First integer = 45
Second integer = 23

Swapping of two numbers without third variable

You can also swap two numbers without using third variable. In that case C program will be as follows:

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.    int a, b;
  6.    
  7.    printf("Input two integers (a & b) to swap\n");
  8.    scanf("%d%d", &a, &b);
  9.    
  10.    a = a + b;
  11.    b = a - b;
  12.    a = a - b;
  13.  
  14.    printf("a = %d\nb = %d\n",a,b);
  15.    return 0;
  16. }

Output of program:
Swap numbers C program output

To understand the logic choose the variables 'a' and 'b' as '7' and '9' respectively and then do what is being done by the program. You can choose any other combination of numbers as well. Sometimes it's an excellent way to understand a program.

Swap function in C language

In this method we will make a function to swap numbers. We will use call by reference.

  1. #include <stdio.h>
  2.  
  3. void swap(int*, int*); //Swap function declaration
  4.  
  5. int main()
  6. {
  7.    int x, y;
  8.  
  9.    printf("Enter the value of x and y\n");
  10.    scanf("%d%d",&x,&y);
  11.  
  12.    printf("Before Swapping\nx = %d\ny = %d\n", x, y);
  13.  
  14.    swap(&x, &y);
  15.  
  16.    printf("After Swapping\nx = %d\ny = %d\n", x, y);
  17.  
  18.    return 0;
  19. }
  20. //Swap function definition
  21. void swap(int *a, int *b)
  22. {
  23.    int t;
  24.  
  25.    t  = *b;
  26.    *b = *a;
  27.    *a = t;
  28. }

Swap two numbers using pointers

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.    int x, y, *a, *b, temp;
  6.  
  7.    printf("Enter the value of x and y\n");
  8.    scanf("%d%d", &x, &y);
  9.  
  10.    printf("Before Swapping\nx = %d\ny = %d\n", x, y);
  11.    
  12.    a = &x;
  13.    b = &y;
  14.    
  15.    temp = *b;
  16.    *b   = *a;
  17.    *a   = temp;
  18.  
  19.    printf("After Swapping\nx = %d\ny = %d\n", x, y);
  20.    
  21.    return 0;
  22. }

C programming code to swap using bit-wise XOR

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   int x, y;
  6.  
  7.   scanf("%d%d", &x, &y);
  8.  
  9.   printf("x = %d\ny = %d\n", x, y);
  10.  
  11.   x = x ^ y;
  12.   y = x ^ y;
  13.   x = x ^ y;
  14.  
  15.  
  16.   printf("x = %d\ny = %d\n", x, y);
  17.  
  18.   return 0;
  19. }

Swapping is used in sorting algorithms that is when we wish to arrange numbers in a particular order either in ascending order or in descending order.