用c语言写交换两个数的代码
#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);
printf("Before swapping: a = %d, b = %d\n", a, b);
temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
这段代码中,首先输入两个数a和b,然后用一个临时变量temp来存储a的值,将a赋值为b,再将temp的值赋值给b,从而完成了a和b的交换。最后输出交换后的结果。
原文地址: https://www.cveoy.top/t/topic/by0M 著作权归作者所有。请勿转载和采集!