Write a program to exchange two variables in C without third variable or stack or cpu register.

Source Code

/* xchg.cpp : Exchange two variables in C without third variable or stack or cpu register / */
#include<stdio.h>

int main(int argc, char* argv[])
{
  int a, b;
  a = 2;
  b = 3;
  printf("Enter value for a = ");
  scanf("%d", &a);

  printf("Enter value for b = ");
  scanf("%d", &b);

  printf("Doing exchange with XCHG\n");
  _asm{
   mov eax, a
   xchg eax, b
   mov a, eax
  }
  printf("Values after XCHG a = %d, b = %d\n", a, b);
  return 0;
}


Find More from our code collection
Armstrong number, binary number to a decimal number, bubble sort, decimal number to binary number, factorial of the given number factors, fibonacci numbers, HCF and LCM, matrix, mergesort, salary of the employee. palindrome, quadratic equation, star patterns, series etc. and much more...
#Return to Example Source Code