Section 12: Data exchange between two variables.

Publisher:翠绿山水Latest update time:2016-03-11 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Ten years of experience in the industry, teaching you the 12th lesson of microcontroller introduction:

In order to deepen the understanding of an important feature of assignment statements, "coverage", this section uses the assignment statement "=" to do an experiment. It requires swapping the data of variables a and b. Assume that the original data of a is 1 and the original data of b is 5. After swapping the data, the data of a should become 5 and the data of b should become 1.

When many beginners first see such a simple question, they will take it for granted based on our daily thinking, you give me your things, I give you my things, just two steps, so easy! Please directly copy the template program in Section 10 and only modify the main function. The source code of the main function is as follows:

void main() //Main program

{

/*---The beginning of C language learning area---------------------------------------------------------------------------*/

unsigned char a=1; //Define a variable a and allocate a byte of RAM space, the data stored in it is initialized to 1.

unsigned char b=5; //Define a variable b and allocate a byte of RAM space, the data stored in it is initialized to 5.

b=a; //First step: In order to exchange, first assign the value of a to b.

a=b; //Step 2: For exchange, assign the value of b to a.

GuiWdData0=a; //Put the value of variable a into window variable 0 for display

GuiWdData1=b; //Put the value of variable b into window variable 1 for display

/*---End of C language learning area---------------------------------------------------------------------------*/

while(1)

{

initial();

key_service();

display_service();

}

}

Copy code

Observe the results of program execution on Jianhong 51 learning board:

The value of variable a is 1.

The value of variable b is 1.

The above experimental results did not achieve the purpose of exchanging data. Why? Because assignment statements have an important feature, which is coverage. The analysis is as follows:

b=a; //First step

Analysis and comments: After executing the first step, although b gets a's data 1, b's original data 5 has been overwritten and lost!

a=b; //Step 2

Analysis and comments: Since the data of b becomes 1 after executing the first step, after executing the second step, it is equivalent to assigning 1 to a, and there is no 5! Therefore, the data of a and b are both 1, and the purpose of "a is 5, b is 1" after the exchange cannot be achieved.

The above data exchange program fails! What should I do? Since the assignment statement has coverage, if two variables want to exchange data, they must use a third party to store it. In this case, you only need to define an additional third-party variable t. The source code of the main function is as follows:

void main() //Main program

{

/*---The beginning of C language learning area---------------------------------------------------------------------------*/

unsigned char a=1; //Define a variable a and allocate a byte of RAM space, the data stored in it is initialized to 1.

unsigned char b=5; //Define a variable b and allocate a byte of RAM space, the data stored in it is initialized to 5.

unsigned char t; //Define a variable t and allocate one byte of RAM space. The default data in it is not important.

t=b; //Step 1: To avoid the data of b being overwritten and lost after executing the second step, first store the data of b in the third-party variable t.

b=a; //Step 2: Assign the value of a to b. Although the original data of b is overwritten and lost, b has a backup in the t variable, so there is no need to worry about it.

a=t; //Step 3: Since b has already obtained a's data, if you want to exchange it, you can only assign the backup of b in the t variable to a instead of using b.

GuiWdData0=a; //Put the value of variable a into window variable 0 for display

GuiWdData1=b; //Put the value of variable b into window variable 1 for display

/*---End of C language learning area---------------------------------------------------------------------------*/

while(1)

{

initial();

key_service();

display_service();

}

}

Observe the results of program execution on Jianhong 51 learning board:

The value of variable a is 5.

The value of variable b is 1.

The exchange was successful!

Preview of the next section: Binary and byte units.

Reference address:Section 12: Data exchange between two variables.

Previous article:Section 13: Binary and byte units, and the value ranges of various defined variables
Next article:Section 11: Variable definition and assignment statements

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号