Several questions that embedded programmers need to understand

For future embedded programmers, you want to know if you can become a good engineer. If you want to know how much you know about embedded, then please pay attention to the issues we discuss next.

Preprocessor

1. Declare a constant with the preprocessor directive #define to indicate how many seconds in a year (ignoring the leap year problem)

#define SECONDS_PER_YEAR (60 * 60 * 24 * 365)UL

I want to see a few things here:

1). #define Basic knowledge of grammar (for example: can't end with a semicolon, use parentheses, etc.)

2). Knowing that the preprocessor will calculate the value of a constant expression for you, it is clearer and without cost to write directly how you calculate how many seconds in a year instead of calculating the actual value.

3). Realize that this expression will overflow an integer of a 16-bit machine - so use the long integer L to tell the compiler that the constant is a long integer.

4). If you use UL (for unsigned long integers) in your expression, then you have a good starting point. Remember, the first impression is important.

2. Write a "standard" macro MIN that takes two arguments and returns the smaller one.

#define MIN(A,B) ((A) <<= (B) ? (A) : (B))

1). Identifies the basics of #define application in macros. This is important because macros are the only way to easily generate embedded code until the inline operator becomes part of standard C. For embedded systems, embedding code is often necessary to achieve the required performance. Methods.

2). Knowledge of the triple condition operator. The reason this operator exists in C is that it allows the compiler to produce code that is more optimized than if-then-else. It is important to understand this usage.

3). Understand the parameters in parentheses carefully

4). I also started to discuss the side effects of macros with this question, for example: What happens when you write the following code? Least = MIN(*p++, b);

3. What is the purpose of the preprocessor identification #error?

If you don't know the answer, see Reference 1. This question is useful for distinguishing between a normal buddy and a nerd. Only nerds can read the appendix of the C language textbook to find answers to such questions. Of course, if you are not looking for a nerd, then candidates should hope that they do not know the answer.

Infinite loops

4. Infinite loops are often used in embedded systems. How do you write infinite loops in C?

This problem uses several solutions. My preferred solution is:

While(1)

{

}

Some programmers prefer the following solutions:

For(;)

{

}

This implementation makes me embarrassed because this grammar does not exactly express what is going on. If a candidate gives this as a solution, I will use this as an opportunity to explore the basic principles of doing so. If their basic answer is: "I was taught to do this, but never thought about why." This will leave me a bad impression.

The third option is to use goto

Loop:

. . .

Goto Loop;

The candidate gives the above solution, which means that either he is an assembly language programmer (which may be a good thing) or he is a BASIC/FORTRAN programmer who wants to enter a new field.

Data declaration (Data declaraTIons)

5. Use variable a to give the following definition

a) An integer (An integer)

b) A pointer to an integer (A pointer to an integer)

c) a pointer to a pointer pointing to an integer (A pointer to a pointer to an integer)

d) An array of 10 integers (An array of 10 integers)

e) an array of 10 pointers pointing to an integer (An array of 10 pointers to integers)

f) A pointer to an array of 10 integers (A pointer to an array of 10 integers)

g) a pointer to a function that has an integer argument and returns an integer (A pointer to a funcTIon that takes an integer as an argument and returns an integer)

h) an array of 10 pointers to a function that has an integer argument and returns an integer (An array of ten pointers to funcTIons that take an integer argument and return an integer )

the answer is:

a) int a; // An integer

b) int *a; // A pointer to an integer

c) int **a; // A pointer to a pointer to an integer

d) int a[10]; // An array of 10 integers

e) int *a[10]; // An array of 10 pointers to integers

f) int (*a)[10]; // A pointer to an array of 10 integers

g) int (*a)(int); // A pointer to a funcTIon a that takes an integer argument and returns an integer

h) int (*a[10])(int); // An array of 10 pointers to functions that take an integer argument and return an integer

People often claim that there are several problems here that are the kind of questions that can be answered by flipping through the book. I agree with this statement. When I was writing this article, in order to determine the correctness of the grammar, I did check the book. But when I was interviewed, I expected to be asked this question (or a similar question). Because during the interview, I am sure I know the answer to this question. If the candidate does not know all the answers (or at least most of the answers), then there is no preparation for this interview. If the interviewer is not prepared for the interview, why can he prepare?

Static

6. What is the role of the keyword static?

Few people can answer this simple question completely. In C, the keyword static has three distinct effects:

1). In the body of a function, a variable declared to be static maintains its value during the call to this function.

2). Within a module (but outside the function), a variable declared to be static can be accessed by functions used within the module, but not by functions outside the module. It is a local global variable.

3). Within a module, a function declared to be static can only be called by other functions within this module. That is, this function is restricted to use within the local scope of the module that declares it.

Most candidates can correctly answer the first part, some can answer the second part correctly, and few people can understand the third part. This is a serious shortcoming of the candidate because he obviously does not understand the benefits and importance of localizing the data and code scope.

Const

7. What is the meaning of the keyword const?

As soon as I heard the interviewee say "const means constant", I know that I am dealing with an amateur. Last year, Dan Saks has fully summarized all the uses of const in his article, so every reader of ESP (Embedded Systems Programming) should be very familiar with what const can and can't do. If you have never read that article, just say that const means "read-only". Although this answer is not a complete answer, I accept it as a correct answer. (If you want to know a more detailed answer, read the article by Saks.) If the candidate can answer this question correctly, I will ask him an additional question: What do the following statements mean?

Const int a;

Int const a;

Const int *a;

Int * const a;

Int const * a const;

The first two functions are the same, a is a constant integer. The third means that a is a pointer to a constant integer (that is, the integer is not modifiable, but the pointer can be). The fourth meaning a is a constant pointer to an integer (that is, the integer pointed to by the pointer can be modified, but the pointer is not modifiable). The last one means that a is a constant pointer to a constant integer (that is, the integer pointed to by the pointer is not modifiable, and the pointer is also unmodifiable). If the candidate can answer these questions correctly, then he left a good impression on me. Incidentally, perhaps you may ask, even if you don't use the keyword const, you can easily write a program with the correct function, so why should I value the keyword const? I also have the following reasons:

1). The purpose of the keyword const is to convey very useful information to the person reading your code. In fact, declaring a parameter as a constant is to tell the user the purpose of the application. If you have spent a lot of time cleaning up the rubbish left by others, you will quickly learn to thank this extra information. (Of course, programmers who know how to use const will rarely leave garbage for others to clean up.)

2). Using the keyword const may produce more compact code by giving the optimizer some additional information.

3). Proper use of the keyword const allows the compiler to naturally protect parameters that are not desired to be changed, preventing them from being modified by unintentional code. In short, this can reduce the occurrence of bugs.

Volatile

8. What is the meaning of the keyword volatile? And give three different examples.

A variable defined as volatile means that the variable may be unexpectedly changed so that the compiler does not assume the value of the variable. To be precise, the optimizer must carefully re-read the value of this variable each time it uses this variable instead of using the backup stored in the register. Here are a few examples of volatile variables:

1). Hardware registers of parallel devices (eg status registers)

2). Non-automatic variables that are accessed in an interrupt service routine (Non-automatic variables)

3). Variables shared by several tasks in a multi-threaded application

Those who cannot answer this question will not be hired. I think this is the most basic problem that distinguishes between C programmers and embedded system programmers. Embedded system programmers often deal with hardware, interrupts, RTOS, etc., all of which require volatile variables. Not knowing that volatile content will bring disaster. Suppose the interviewee correctly answers this question (well, doubt if this will be the case), I will take a closer look and see if this guy understands the importance of volatile.

1). Can a parameter be const or volatile? Explain why.

2). Can a pointer be volatile? Explain why.

3). What's wrong with the following function:

Int square(volatile int *ptr)

{

Return *ptr * *ptr;

}

Here is the answer:

1). Yes. An example is a read-only status register. It is volatile because it can be unexpectedly changed. It is const because the program should not try to modify it.

2). Yes. Although this is not very common. An example is when a service subroutine modifies a pointer to a buffer.

3). There is a prank in this code. The purpose of this code is to return the pointer *ptr to the square of the value, but since *ptr points to a volatile parameter, the compiler will produce code similar to the following:

Int square(volatile int *ptr)

{

Int a,b;

a = *ptr;

b = *ptr;

Return a * b;

}

Since the value of *ptr may be unexpectedly changed, a and b may be different. As a result, this code may not be the squared value you expected! The correct code is as follows:

Long square(volatile int *ptr)

{

Int a;

a = *ptr;

Return a * a;

}

 The Bluetooth headset is to apply Bluetooth technology to the handsfree earphone, so that users can avoid the annoying wires and tie, and when and where they can easily talk.  Grand's Bluetooth earphone is small, wireless, portable, and other features, waterproof, Bluetooth stereo earphone, single-ear wireless Bluetooth earphone and hands-free wireless Bluetooth earphone.

Wireless earphone

Wireless earphone,Mono Earphone, Wireless Bluetooth Earphone

Shenzhen PuFang Technology Co., Ltd. , https://www.hytelus.com