How to swap values with subtraction and addition in C

1 Answer

0 votes
#include <stdio.h>

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

    SWAP(x, y);
  
    printf("x = %d y = %d\n", x, y);
      
    return 0;
}
  
  
  
  
/*
run:

x = 1000 y = 8290

*/

 



answered Apr 10, 2024 by avibootz

Related questions

1 answer 112 views
1 answer 105 views
105 views asked May 12, 2023 by avibootz
1 answer 158 views
1 answer 106 views
106 views asked May 12, 2023 by avibootz
1 answer 109 views
109 views asked Apr 19, 2023 by avibootz
1 answer 146 views
...