Swap values of two variables in C using XOR

Swapping of Two Numbers in C without using XOR: 

First, see the: Swapping of  two numbers in C

Swapping of Two number program in C using XOR:
int main()
 int a;
 int b;
 printf("Type value of A = ");
 printf("\nType value of B = ");
  a ^= b;
  b ^= a;
  a ^= b;

 printf("A = %d",a);
 printf("\nB : %d",b);
 return 0;
Type value of A = 7
Type value of B = 5
A = 5
B = 7
  • Two integers a, b is declared of Type int.
  • The values of a, b are taken from the keyboard given by the user.
  • Now both the values of "a" and "b" are changed using Xor.
    Swapping values of two variables in C using XOR
  • And last Printf Prints the value of “a” and “b”.
See also:

Our Services

Search This Blog

  • ()
Powered by Blogger.

C Variables and Types of Variables in C

C variable is a data name that may be used to store a data value. The value of the C variable may get change in the program and it can be r...

Blog Archive