Embedded Engineer Classic Face question

Source: Internet
Author: User

Http://bbs.elecfans.com/jishu_528921_1_1.html

Last week, to Shenzhen, an arm development company interview, HR asked me to do a paper, inside are C programming, heart Anxi, because these questions are basically in the programmer interview book. Later back to school, on-line search, originally these questions are embedded engineers of the classic interview topics, many sites can be found. Now put him out, attached to the online answer, to share with you, because these questions are too classic.
Preprocessor (Preprocessor)

1. Declare a constant with pre-processing instruction # define to indicate how many seconds are in 1 (ignoring leap year issues)
#define SECONDS_PER_YEAR (* * 365) UL
I want to see a few things here:
1) basic knowledge of #define grammar (for example: cannot end with semicolons, use of parentheses, etc.)
2) know that the preprocessor will calculate the value of the constant expression for you, so it is clearer and less expensive to write directly how many seconds in a year instead of calculating the actual value.
3) realize that this expression will overflow the integer number of a 16-bit machine-so use the long integer symbol l to tell the compiler that this constant is a long integer number.
4) If you are using UL (for unsigned long integers) in your expression, then you have a good starting point. Remember, first impressions are important.

2. Write a "standard" macro min, this macro enters two parameters and returns the smaller one.
#define MIN (A) (A) <= (B)? (A): (B))
This test is designed for the following purposes:
1) identifies the basic knowledge that # define is applied in the macro. This is very important. Because macros are the only way to generate embedded code before the embedded (inline) operator becomes part of standard C, embedding code is often a necessary method for embedded systems to achieve the required performance.
2) knowledge of the triple conditional operator. The reason that this operator exists in the C language is that it allows the compiler to produce better code than If-then-else, and it is important to understand this usage.
3) Be careful to enclose the parameters in parentheses in the macro.
4) I also use this issue to start discussing the side effects of macros, for example: What happens when you write the following code.
least = MIN (*p++, b);

3. What is the purpose of the preprocessor identity #error.
If you don't know the answer, see reference 1. This question is useful for distinguishing between a normal guy and a nerd. Only nerds can read the appendix to the C language textbook to find answers like this. Of course, if you're not looking for a nerd, then it's best for a candidate to want to not know the answer.

Dead Loop (Infinite loops)

4. An infinite loop is often used in embedded systems, how do you write a dead loop in C?
This problem is solved with several solutions. My preferred solution is to:

while (1)
{

}

Some programmers prefer the following scenarios:

for (;;)
{

}

This implementation is embarrassing for me because the syntax doesn't exactly say what's going on. If a candidate gives this as a plan, I'll use this as an opportunity to explore the fundamentals of what they do. If their basic answer is: "I was taught to do this, but never thought of why." "It will leave a bad impression on me.

The third option is to use a goto
Loop:
...
Goto Loop;
If the candidate gives the above scenario, it 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 statement (declarations)

5. Use variable A to give the following definition
A) An integer number (an integer)
b) A pointer to the integer number (a pointer to an integer)
c) A pointer to a pointer to a pointer that points to a number of integers (a pointer to a pointer to an intege) r
d) An array of 10 integers (an arrays of ten integers)
e) An array of 10 pointers that point to an integer number. (An array of ten pointers to integers)
f) A pointer to the array of 10 integer numbers (a pointer to an array of ten integers)
g) A pointer to the function that has an integer parameter and returns an integer number (a pointer to a function this takes an integer as an argument and returns an integer)
h) An array of 10 pointers that point to a function that has an integer parameter and returns an integer number (an array of ten pointers to functions that taking an integer argument and retur n 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 ten integers
e) int *a[10]; An array of ten pointers to integers
f) Int (*a) [10]; A pointer to an array of ten integers
g) Int (*a) (int); A pointer to a function A, takes an integer argument and returns an integer
h) int (*a[10]) (int); An array of ten pointers to functions A, a integer argument and return an integer

I agree that there are a number of questions that people often claim are the kind of questions that need to be answered by flipping a book. When I wrote this article, I did check the book in order to determine the correctness of the grammar. But when I was interviewed, I expected to be asked the question (or similar question). Because during the interview, I was sure I knew the answer to the question. If the candidate does not know all the answers (or at least most of the answers), then there is no preparation for the interview, and if the interviewer does not prepare for the interview, then why should he be prepared.


6. What is the role of the keyword static?
Few people can answer this simple question completely. In the C language, the keyword static has three distinct effects:
1) in the function body, a variable declared as static maintains its value in the process of the function being called.
2) within the module (but outside the function body), a variable declared as static can be accessed by a function within the module, but not by other functions outside the module. It is a local global variable.
3) within the module, a function declared as static can only be called by other functions within this module. That is, this function is restricted to the local scope of the module that declares it.

Most candidates are able to answer the first part correctly, and part of them can answer the second part correctly, with very few people able to understand the third part. This is a serious drawback for a candidate because he clearly does not understand the benefits and importance of localizing 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 knew I was dealing with an amateur. Last year Dan Saks has completely summed up all the usages of const in his article, so ESP (translator: Embedded Systems programming) Every reader should be very familiar with what const can and cannot do. If you've never read that article, you can 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 more about the answer, read Saks's article carefully.) )
If the candidate can answer the question correctly, I will ask him an additional question:
What is the meaning of the following statement?

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 number. The third means that a is a pointer to a constant integer number (that is, the integer number is not modifiable, but the pointer can). The fourth meaning a is a constant pointer to an integer number (that is, the integer number pointed to by the pointer can be modified, but the pointer is non-modifiable). The last one means that a is a constant pointer to a constant number (that is, the integer number pointed to by the pointer is not modifiable and the pointer is not modifiable). If the candidate can answer these questions correctly, then he leaves a good impression on me. Incidentally, perhaps you may ask, even without the keyword const, it is easy to write the function of the correct program, then why I value the keyword const. I also have the following several reasons:
1) The function of the keyword const is to convey very useful information to the person who is reading your code, and in fact, declaring a parameter as a constant is to tell the user the purpose of the application of this parameter. If you have spent a lot of time cleaning up the rubbish left by others, you will soon learn to thank for the extra information. (Of course, it's very rare for a const programmer to leave rubbish for others to clean up.) )
2) using the keyword const may produce more compact code by giving the optimizer some additional information.
3) The use of the keyword const makes it natural for the compiler to protect those parameters that you do not want to change, and to prevent them from being unintentionally modified. In short, this can reduce the occurrence of bugs.

Volatile

8. What does the keyword volatile mean? and gives three different examples.
A variable that is defined as volatile means that the variable may be unexpectedly changed so that the compiler does not assume the value of the variable. Precisely, the optimizer must carefully re-read the value of the variable each time it uses the variable, rather than using the backup stored in the register. Here are a few examples of volatile variables:
1) Hardware registers for parallel devices (e.g., status registers)
2) non-automatic variables that are accessed in an interrupt service subroutine (non-automatic variables)
3) variables shared by several tasks in multi-threaded applications

The person who cannot answer the question will not be hired. I think this is the most basic problem of distinguishing between C programmers and embedded system programmers. Embedded guys often deal with hardware, interrupts, RTOs, and so on, all of which require volatile variables. The content that does not understand volatile will bring disaster.
Assuming that the interviewee has answered the question correctly (well, wondering if this is the case), I'll look at it a little bit and see if this guy is straight on about the importance of volatile.
1) can a parameter be either const or volatile? explain why.
2) A pointer can be volatile. explain why.
3) What is wrong with the following function:

int square (volatile int *ptr)
{
return *ptr * *PTR;
}

Here's the answer:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.