Home  >  Article  >  Backend Development  >  C language to exchange the value of two numbers

C language to exchange the value of two numbers

angryTom
angryTomOriginal
2019-10-23 13:17:3415448browse

C language to exchange the value of two numbers

C language exchanges the values ​​​​of two numbers

There are two numbers a, b in the program, where a =4,b=5, now I want to exchange the values ​​of the two numbers so that a=5,b=4.

Recommended course: C Language Tutorial

Method 1: Use a temporary variable to save, this should be the easiest method for everyone to think of

Reference code:

#include <stdio.h>
int main()
{
    int a = 4;
    int b = 5;
    int nTemp;
    printf("交换前a,b的值为:\n");
    printf("a=%d\n", a);
    printf("b=%d\n", b);
    printf("\n");
    nTemp = a;
    a = b;
    b = nTemp;
    printf("----------------------------------我是分割线------------------------------------\n");
    printf("交换前a,b的值为:\n");
    printf("a=%d\n", a);
    printf("b=%d\n", b);
    return 0;
}

Running results:

C language to exchange the value of two numbers

Method 2: Use addition and subtraction to exchange, this method is actually to temporarily save the result of a b in the variable a, and then change it By subtracting a from the original b, you can get the exchanged b. However, this method has a flaw, that is, both a and b are of type int, and the result of a b can go out of bounds.

Reference code:

#include <stdio.h>
int main()
{
    int a = 4;
    int b = 5;
    printf("交换前a,b的值为:\n");
    printf("a=%d\n", a);
    printf("b=%d\n", b);
    printf("\n");
    a = a + b;
    b = a - b;
    a = a - b;
    printf("----------------------------------我是分割线------------------------------------\n");
    printf("交换前a,b的值为:\n");
    printf("a=%d\n", a);
    printf("b=%d\n", b);
    return 0;
}

Run result:

C language to exchange the value of two numbers

Method 3: Use multiplication and division to exchange, this method is similar to method 2, except that a*b is used instead of a and b to be temporarily stored in a. The following ideas are the same. This method has the same defect as method 2, which is the problem of out-of-bounds, and it is easier to cross-bound.

Reference code:

#include <stdio.h>
int main()
{
    int a = 4;
    int b = 5;
    printf("交换前a,b的值为:\n");
    printf("a=%d\n", a);
    printf("b=%d\n", b);
    printf("\n");
    a = a * b;
    b = a / b;
    a = a / b;
    printf("----------------------------------我是分割线------------------------------------\n");
    printf("交换前a,b的值为:\n");
    printf("a=%d\n", a);
    printf("b=%d\n", b);
    return 0;
}

Method 4: Use the XOR method to exchange . This method does not have the previous out-of-bounds defect. It is a perfect method. This method mainly takes advantage of the XOR feature to save the value of a^b to a first. Because a^b^b=a, the exchange can be completed smoothly.

Reference code:

#include <stdio.h>
int main()
{
    int a = 4;
    int b = 5;
    printf("交换前a,b的值为:\n");
    printf("a=%d\n", a);
    printf("b=%d\n", b);
    printf("\n");
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
    printf("----------------------------------我是分割线------------------------------------\n");
    printf("交换前a,b的值为:\n");
    printf("a=%d\n", a);
    printf("b=%d\n", b);
    return 0;
}

Method 5: Use the shift method to split the original int type a into high values 16 bits and the lower 16 bits are equivalent to 16 more bits of temporary storage space that can be turned around, but the flaw of this method is also obvious, that is, if the assigned a or b exceeds 16 bits, this method will cause an error.

Reference code:

#include <stdio.h>
int main()
{
    int a = 32123;
    int b = 12345;
    printf("交换前a,b的值为:\n");
    printf("a=%d\n", a);
    printf("b=%d\n", b);
    printf("\n");
    a <<= 16;
    a |= b;
    b = a >> 16;
    a = a & 0xffff;
    printf("----------------------------------我是分割线------------------------------------\n");
    printf("交换前a,b的值为:\n");
    printf("a=%d\n", a);
    printf("b=%d\n", b);
    return 0;
}

Running result:

C language to exchange the value of two numbers

The above is the detailed content of C language to exchange the value of two numbers. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn