C——swap

/* swap.c */
/* function swap to swap two numbers */

#include <stdio.h>

void swap(int*, int*);
void swapBit(int*, int*);

int main(){
    int x, y;

    printf("Please enter two numbers: ");    
    scanf("%d %d", &x, &y);
    
    printf("Before: ");
    printf("x, y = %d, %d\n", x, y);

    swap(&x, &y);
    printf("After swap(): ");
    printf("x, y = %d, %d\n", x, y);

    swapBit(&x, &y);
    printf("After swapBit(): ");
    printf("x, y = %d, %d\n", x, y);

    return 0;
}

void swap(int* x, int* y){
    int tmp = *x;
    *x = *y;
    *y = tmp;    
}

void swapBit(int* x, int* y){
    *x ^= *y;
    *y ^= *x;
    *x ^= *y;
}

 

猜你喜欢

转载自www.cnblogs.com/noonjuan/p/11481041.html