Sau đây là một ví dụ về hoán đổi hai biến.
Ví dụ
#include <stdio.h>
int main() {
int a,b;
printf("Enter the value of a : ");
scanf("%d", &a);
printf("\nEnter the value of b : ");
scanf("%d", &b);
a += b -= a = b - a;
printf("\nAfter Swapping : %d\t%d", a, b);
return 0;
} Đầu ra
Enter the value of a : 23 Enter the value of b : 43 After Swapping : 4323
Trong chương trình trên, hai biến a và b được khai báo và khởi tạo động tại thời điểm chạy.
int a,b;
printf("Enter the value of a : ");
scanf("%d", &a);
printf("\nEnter the value of b : ");
scanf("%d", &b); Các số được hoán đổi mà không sử dụng bất kỳ biến thứ ba nào.
a += b -= a = b - a;