1. Declare a constant with preprocessing instruction #define to show how many seconds there are in 1 year (ignoring the problem of boudoir year).
# define sec _ year (365 * 24 * 60 * 60) ul detection point:
Basic knowledge of 1 # Define the syntax (for example, you can't end with a semicolon, the use of parentheses, etc. )
2. Know that the preprocessor will help you calculate the value of the constant expression, so it is clearer and more cost-free to write directly how you calculate how many seconds there are in a year instead of calculating the actual value.
Realize that this expression will overflow an integer of 16-bit machine-so use the long integer symbol l to tell the compiler that this constant is a long integer.
If you use UL (unsigned long integer) in your expression, you have a good starting point. Remember, the first impression is very important.
2. Write a "standard" macro MIN which inputs two parameters and returns the smaller one.
# define MIN(a, b) ((a) <; =(b)? (a):(b))
Checkpoint: 1. Basic knowledge of identifying #define in macro application. This is very important. Because macros are the only way to generate embedded code conveniently before inline operators become a part of standard C language, embedded code is usually a necessary way for embedded systems to achieve the required performance.
2. Know how to carefully enclose parameters in parentheses in macros.
I also used this question to discuss the side effects of macros.
3. What is the purpose of preprocessor identification #error?
When compiling a program, whenever you encounter #error, a compilation error will pop up. Since it is a compilation error, why? Its purpose is to ensure that the program compiles as you imagine.
#ifdef XXX...# else # Dean When the program is large, some macro definitions are often specified externally (such as makefile) or in a system disaster. When you are not sure whether XXX is currently defined, you can compile it as follows:
# ifdef XXX # Error "XXX has been defined" .. #else#endif
In this way, if there is an error during compilation, XXX has been defined, indicating that the macro XXX has been defined.
4. Infinite loop is often used in embedded systems. How to write infinite loop in C language?
while( 1)
for(; ; )
Cycle: ...
Go to loop;
5. Use the variable A to give the following definition.
A) integer (an integer)
A pointer to an integer.
C) Pointer, pointer to integer (pointer to integer) d)d) 10 array of integers.
E) An array with 10 pointers, which point to an integer. (10 array of integer pointers) f) 10 array of integer pointers.
6. What is the function of the keyword static?
In C language, the keyword static has three obvious functions:
First, when modifying variables, the static modification of static local variables is only performed once, and the life cycle of local variables is extended to the end of the process.
Second, when static modifies a global variable, the global variable can only be accessed in this file, but not in other files, even if it is declared by extern.
Third, static modifies a function, so the function can only be called in this file, not by other files. Static modified local variables are stored in the static variable area of the global data area. Automatically initialized to 0 during initialization;
7. What does the keyword const mean?
As long as a variable is modified by const, it means that the data in the variable can only be accessed and cannot be modified, which means that const is "read-only".
Rule: const can't modify anyone who is close to it;
When const modifies a variable, it must be initialized. If it is not initialized, it cannot be initialized later.
8. What does the keyword volatile mean? And give three different examples.
A variable defined as volatile means that the variable may be changed accidentally, so the compiler will not adopt the value of the variable. To be precise, the optimizer must carefully re-read the value of this variable every time it is used, instead of using the backup stored in the register. Here are some examples of variable variables:
1: Hardware registers (such as status registers) of parallel devices.
2. Non-automatic variables to be accessed in the interrupt service subroutine.
3. Variables shared by multiple tasks in multithreaded applications.