How to swap values with subtraction and addition in C++

1 Answer

0 votes
#include <iostream>

#define SWAP(a, b) ((&(a) == &(b)) || \
                    (((a) -= (b)), ((b) += (a)), ((a) = (b) - (a))))
                    
int main(void) {
    int x = 8290, y = 1000;              

    SWAP(x, y);
  
    std::cout << "x = " << x << " y = " << y;
}
  
  
  
  
/*
run:

x = 1000 y = 8290

*/

 



answered Apr 10, 2024 by avibootz

Related questions

1 answer 124 views
2 answers 158 views
1 answer 114 views
114 views asked Apr 19, 2023 by avibootz
1 answer 105 views
105 views asked May 12, 2023 by avibootz
1 answer 141 views
1 answer 207 views
...