fork download
  1. #include <stdio.h>
  2.  
  3. // Function to swap two numbers using pointers
  4. void swap(int *x, int *y) {
  5. int temp;
  6.  
  7. temp = *x; // store value of x
  8. *x = *y; // put y's value into x
  9. *y = temp; // put temp's (old x) value into y
  10. }
  11.  
  12. int main() {
  13. int a = 5, b = 10;
  14.  
  15. printf("Before swap: a = %d, b = %d\n", a, b);
  16.  
  17. swap(&a, &b); // pass the addresses of a and b
  18.  
  19. printf("After swap: a = %d, b = %d\n", a, b);
  20.  
  21. return 0;
  22. }
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Before swap: a = 5, b = 10
After swap:  a = 10, b = 5