Related articles:
The background of C language embedded system programming cultivation
The software of C language embedded system programming cultivation
C Language Embedded system programming cultivation of memory
C Language Embedded system programming cultivation screen
C Language Embedded system programming the keyboard of the cultivation
1. Use a macro definition
In C, macros are the only way to generate inline code. For embedded systems, macros are a good substitute for functions in order to achieve performance requirements.
Write a "standard" macro min, this macro enters two parameters and returns the smaller one:
Error procedure:
#define MIN(A,B)( A <= B ? A : B )
Correct procedure:
#define MIN(A,B)((A)<= (B)? (A): (B))
For macro, we need to know three points:
(1) macro defines "image" function;
(2) The macro definition is not a function and therefore requires all "parameters" to be enclosed;
(3) Macro definitions can have side effects.
The following code:
least = MIN(*p++, b);
will be replaced by:( (*p++)<= (b)?(*p++):(b))
What happened was unpredictable.
Therefore, do not give macros a "parameter" that has side effects passed in.