1. About while(1) and for(;;) :

Functionally, both are implemented in an infinite loop, but whether code efficiency is exactly the same depends on the compiler. Some compilers compile code, for(;;) Is more efficient than while(1), and some compilers compile exactly the same. For portability and efficiency, use for(;) is recommended. Cycle.

2. Don’t confuse the concept of “bitfield” with the “bit” data type:

In the C standard, there are “bit-field” data structures to save memory space, but there are no bit data types. The bit data type is a data type extended by the compiler in embedded development tools, also to save memory space (the bit data type is only 1 bit). In addition to bit, there are sBIT, SFR, and SFR16.

3. Don’t forget to add volatile to the loop variable:

If compiler tuning is enabled, the compiler in an embedded development environment will often optimize the loop variable to cause errors. In this case, the keyword volatile is used to tell the compiler not to optimize the loop variable.

When reading a variable, the compiler optimizes to read the variable into a register to speed up the reading, and then reads the value directly from the register. A variable decorated with voletile reloads its contents from memory rather than copying them directly from the register.

Note also that the loop variables here are not just for loops, but while loops, such as:

volatile unsigned short i;
for(i=0; i<1000; i++) { ...... }volatile unsigned short j = 2000;
while(j--)
{
    ......
}
Copy the code