程序代码
1.
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a, b, c;
printf("输入两个整形变量的值:");
scanf("%d %d", &a, &b);
{
c = a;
a = b;
b = c;
}
printf("%d %d", a, b);
system("pause");
return 0;
}
2.附加题:不允许创建临时变量,交换两个整数的值
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a = 9;
int b = 3;
a = a + b;
b = a - b;
a = a - b;
printf("%d %d\n ",a,b);
system("pause");
return 0;
}
注释:程序2直接在程序里修改啊,a,b的值
3.运用按位异或(^)位操作符进行交换
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a = 7;
int b = 10;
int tmp;
a = a^b;
b = a^b;
a = a^b;
printf("%d %d", a, b);
system("pause");
return 0;
}