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.
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
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- LLC resonance start-up conditions
- MLX90640 infrared imaging - red eye camera (Chinese information/development notes/source code test)
- I would like to ask, what is the inductor routing problem of this buck power supply circuit?
- GPIO configuration and usage process of CC1310SimpleLink SDK under CCS8
- "Basics of Power Supply Design" - a reference book worth putting on your workbench
- TMP61 ±1% 10kΩ Linear Thermistor
- 【ESP32-C3-DevKitM-1】ESP32-C3 reads the temperature and humidity values of the SHT3X temperature and humidity sensor
- Chapter 8 Timer + DMA driver WS2812B
- Research on Color Interpolation Algorithm of CCD Image and Its FPGA Implementation
- Using R329 to identify QR codes