以下是交换两个变量的示例。
#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;
}输出结果
Enter the value of a : 23 Enter the value of b : 43 After Swapping : 4323
在上面的程序中,声明了两个变量a和b,并在运行时对其进行了动态初始化。
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;