C Programming: Compare Three Numbers

In the C language, the swap function can be used to compare the sizes of three numbers. Here is an example code:

#include <stdio.h>

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

int main() {
    int num1, num2, num3;

    printf("请输入三个整数:");
    scanf("%d %d %d", &num1, &num2, &num3);

    if (num1 > num2) {
        swap(&num1, &num2);
    }
    if (num1 > num3) {
        swap(&num1, &num3);
    }
    if (num2 > num3) {
        swap(&num2, &num3);
    }

    printf("从小到大的顺序为:%d %d %d\n", num1, num2, num3);
    
    return 0;
}

The code above defines a swap function that is used to exchange the values of two integers. In the main function, three integers are first input. Then, three if statements are used to compare the sizes of these three numbers and the swap function is called to ensure that their order is from smallest to largest. Finally, the output is in ascending order.

bannerAds