write a C program to swap two number 数据要手动输入
时间: 2024-03-21 09:42:24 浏览: 151
C语言程序设计之交换两个数的值
Certainly! Here's a modified version of the program that allows the user to input the two numbers to swap:
```c
#include <stdio.h>
void swap(int *a, int *b);
int main() {
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
// call the swap function to swap the values
swap(&num1, &num2);
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
```
In this program, we use the `scanf` function to read in two integers from the user, which are stored in the variables `num1` and `num2`. Then, we print out the original values of the variables before swapping them. The `swap` function is the same as in the previous program, which takes in two integer pointers and swaps their values. Finally, we print out the swapped values to confirm that the program works as expected.
I hope that helps! Let me know if you have any further questions.
阅读全文