下面是一个用C语言编写的函数,可以实现两个数的交换:

#include <stdio.h>

void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int num1, num2;

    printf('请输入两个整数:');
    scanf('%d %d', &num1, &num2);

    printf('交换前的数:num1 = %d, num2 = %d\n', num1, num2);

    swap(&num1, &num2);

    printf('交换后的数:num1 = %d, num2 = %d\n', num1, num2);

    return 0;
}

这个程序中的swap函数接受两个指针作为参数,通过指针来交换两个数的值。在main函数中,首先输入两个整数,然后调用swap函数来交换这两个数的值,最后输出交换后的结果。

C语言实现两个数交换的子函数

原文地址: https://www.cveoy.top/t/topic/pGdz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录