Home  >  Article  >  Backend Development  >  [C Language] 5 tips to teach you how to exchange variable values ​​easily

[C Language] 5 tips to teach you how to exchange variable values ​​easily

little bottle
little bottleforward
2019-04-27 11:15:283621browse

The operation of exchanging the values ​​of two variables is often used in C language. Many people also ask questions such as how to exchange the values ​​​​of two variables without using temporary variables. Today we will make a summary here. There are 5 methods in total. I hope you all gain something from reading it.

1. Temporary variable method

Method explanation: Using the temporary variable method as an intermediate transition, this is the simplest and easiest method for everyone to think of. This method is used in various There are many applications and should be mastered proficiently.

#include<stdio.h>
int main(void){
     int a=1,b=2,tmp;     
     tmp =a;     
     a=b;     
     b=tmp;     
    printf("%d %d",a,b);
     return 0; 
}

2. Addition and subtraction exchange method

Method explanation: As the name suggests, use the addition and subtraction method in mathematics to perform exchange. First, temporarily save the results of a b in the variable a, and then pass this The exchanged b can be obtained by subtracting the changed a from the original b. The disadvantage is that the result of a b may be out of bounds.

#include<stdio.h>
int main(void){     
       int a=1,b=2;
     a = a + b;
     b = a - b;
     a = a - b;     
     printf("%d %d",a,b);     
     return 0;
 }

3. Multiplication and division exchange method

Method explanation: This method is similar to addition and subtraction, that is, addition and subtraction are replaced by multiplication and division. The disadvantage is that because multiplication is used, it is easier to cross the boundary than addition. , should be used with caution.

#include<stdio.h>
int main(void){     
       int a=1,b=2;
     a = a * b;
     b = a / b;
     a = a / b;     
     printf("%d %d",a,b);     
     return 0;
 }

4. , because a^b^b=a, the exchange can be completed smoothly. This method is perfect and there is no cross-border problem.

#include<stdio.h>
int main(void){    
 int a=1,b=2;     
 a = a ^ b;     
 b = a ^ b;     
 a = a ^ b;    
 printf("%d %d",a,b);    
 return 0; }

5. Shift exchange method

Explanation of method: This method is not commonly used. The principle is to split the original value of a of type int into high 16 bits and low 16 bits. , which is equivalent to an extra 16 bits of temporary storage space for turnover. The disadvantage is that if a or b exceeds 16 bits, this method will go wrong. This method is more complicated and error-prone, so it is not recommended for everyone to use.

#include<stdio.h>
int main(void){     
int a=1,b=2;
     a <<= 16;
     a |= b;
     b = a >> 16;
     a = a & 0xffff;     
     printf("%d %d",a,b);     
     return 0;
 }

Reflection summary:

In fact, these five methods are essentially divided into three categories, one is temporary variables, the other is addition, subtraction, multiplication and division operations, and the third is bit operations. Because of their respective advantages and disadvantages, I personally recommend that you try to use the temporary variable method and the XOR exchange method. The temporary variable method is also widely used in other places. Let’s focus on it. Let’s stop here. Thank you. Everyone.


Related tutorials:

C language tutorial

The above is the detailed content of [C Language] 5 tips to teach you how to exchange variable values ​​easily. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete
Previous article:What is C# used for?Next article:What is C# used for?