[Recommended] C Language Testing: 0 × 10 basic problems that embedded programmers should know

Source: Internet
Author: User

C language testing is an essential and effective way to recruit embedded system programmers. Over the years, I have both participated in and organized many such tests. During this process, I realized that these tests can provide a lot of useful information for both the interviewer and the interviewee. In addition, aside from the interview pressure, this test is also quite interesting.
From the perspective of the subject, you can learn a lot about the problem or invigilator. Is this test designed to show the ANSI standard details rather than technical skills? Is this stupid question? To obtain the ASCII value of a character. Do these questions focus on your system call and memory allocation policy capabilities? This indicates that the author may spend time on a microcomputer rather than on an embedded system. If the answer to any of the above questions is "yes", I know that I have to seriously consider whether I should do the job.
From the perspective of the interviewer, a test may reveal the examinee's quality in many aspects: the most basic, you can understand the examinee's C language level. No matter how it works, it's interesting to see how this person answers questions he doesn't know. Should the examinee make a wise choice with a good intuition, or is it just blind? When the examinee gets stuck on a problem, is it an excuse? Is it a real curiosity about the problem? Does it look like a learning opportunity? I found this information as useful as their test scores.
With these ideas, I decided to give some questions that really target embedded systems. I hope these headaches can help people who are looking for a job. I have encountered these problems over the years. Some of these questions are difficult, but they should give you some inspiration.
This test is suitable for candidates of different levels. Most primary-level candidates have poor performance, and experienced programmers should have good performance. In order for you to determine the preference of some questions, no score is assigned for each question. If you choose these questions for your use, please assign scores according to your meaning.
Preprocessor)

1. Use the preprocessing command # define to declare a constant to indicate the number of seconds in a year (ignore the leap year problem)

# Define seconds_per_year (60*60*24*365) UL
Here I want to see several things:
& #8226; # basic knowledge of define syntax (for example, it cannot end with a semicolon, use of parentheses, and so on)

& #8226; know how the Preprocessor will calculate the value of a constant expression for you. Therefore, you can directly write how many seconds in a year instead of the actual value, it is clearer with no cost.
& #8226; realize that this expression will overflow the integer number of a 16-bit machine-so the long integer sign L is used to tell the compiler that this constant is the long integer number.
& #8226; if you use ul (representing an 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 a smaller one.

# Define min (A, B) (a) <= (B )? (A): (B ))

This test is intended for the following purposes:
& #8226; identifier # basic knowledge of define application in macros. This is important, because until the inline operator becomes part of standard C, macros are the only way to facilitate the generation of embedded code. For embedded systems, in order to achieve the required performance, embedded code is often a required method.
& #8226; triplicate operator knowledge. The reason for the existence of this operator in C language is that it enables the compiler to produce code that is more optimized than if-then-else. It is important to understand this usage.
& #8226; carefully enclose parameters in brackets in macros
& #8226; I also use this question to discuss the side effects of macros. For example, what will happen when you write down the code below?

Least = min (* P ++, B );

3. pre-processor ID # What is the purpose of error?
If you do not know the answer, see References 1. This problem is useful for distinguishing a normal guy from a nerd. Only the nerd can read the appendix of the C language textbook to find the answer to such a question. Of course, if you are not looking for a nerd, you 'd better ask yourself not to know the answer.
Infinite Loops)

4. Infinite loops are often used in embedded systems. How do you write an infinite loop in C?
There are several solutions to this problem. My preferred solution is:

While (1)
{
?}

Some programmers prefer the following solutions:

For (;;)
{
?}

This implementation method makes me embarrassed because this syntax does not exactly express what is going on. If a candidate provides this solution, I will use this as an opportunity to explore the basic principles of their practice. If their basic answer is: "I was taught to do this, but I never thought of why ." This leaves a bad impression on me.
The third solution is to use GOTO

Loop:
...
Goto loop;

If the candidate gives the above solution, it indicates that he is an assembly language programmer (which may be a good thing) or a basic/Fortran programmer who wants to enter a new field.

Data declarations)

5. Use variable A to give the following definition
A) 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 an intege) r
D) an array of 10 integers (an array of 10 integers)
E) an array with 10 pointers pointing to an integer. (An array of 10 pointers to integers)
F) A pointer to an array with 10 integers (a pointer to an array of 10 integers)
G) a pointer to a function. The function has an integer parameter and returns an integer number (a pointer to a function that takes an integer as an argument and returns an integer)
H) An array with 10 pointers pointing to a function, this function has an integer parameter 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 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 questions that can be answered only after a book is opened. I agree with this statement. When I wrote this article, I checked the book to confirm the correctness of the syntax. But when I was interviewed, I expected to be asked this question (or similar questions ). Because I knew the answer to this question during the interview period. If the examinee does not know all the answers (or at least most of the answers), he will not be prepared for the interview. If the interviewer is not prepared for the interview, why should he prepare?
Static
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 obvious functions:
& #8226; In the function body, a variable declared as static keeps its value unchanged when the function is called.
& #8226; in a module (but in vitro), a variable declared as static can be accessed by the function used in the module, but not by other functions outside the module. It is a local global variable.
& #8226; in a module, a function declared as static can only be called by other functions in the module. That is, this function is restricted to use within the local scope of the module that declares it.
Most of the respondents can answer part 1 correctly. Some of them can answer part 2 correctly. Few people can understand part 3. This is a serious disadvantage of a candidate because he obviously does not understand the benefits and importance of localized data and code scope.

Const

7. What does the const keyword mean?
As soon as I hear the subject say, "const means constant", I know that I am dealing with an amateur. Last year, Dan Saks has completely summarized all const usage in his article, So ESP (TRANSLATOR: embedded systems programming) every reader should be very familiar with what const can and cannot do. if you have never read that article, you only need to say that const means "read-only. Although this is not a complete answer, I accept it as a correct answer. (If you want to know more detailed answers, read the Saks Article carefully .)
If the examinee can answer this question correctly, I will ask him an additional question:
What does the following statement mean?

Const int;
Int const;
Const int *;
Int * const;
Int const * a const;

/******/
The first two functions are the same. A is a constant integer. Third, it means that a is a pointer to a constant INTEGER (that is, an integer cannot be modified, but a pointer can be ). Fourth, a is a constant pointer to an integer (that is, the integer to which the Pointer Points can be modified, but the pointer cannot be modified ). The last one means that A is a constant pointer to a constant INTEGER (that is, the pointer to an integer cannot be modified, and the pointer cannot be modified ). If the examinee can answer these questions correctly, he will leave a good impression on me. By the way, you may ask, even if you don't need the keyword const, it is still easy to write a program with the correct function. Why should I pay so much attention to the keyword const? I also have the following reasons:
& #8226; the function of the keyword const is to send very useful information to the person who reads your code. In fact, declaring a parameter as a constant is to tell the user the purpose of applying this parameter. If you spend a lot of time cleaning up the garbage left by others, you will soon learn to thank this excess information. (Of course, programmers who know how to use const seldom leave garbage for others to clean up .)
& #8226; by attaching information to the optimizer, using the keyword const may produce more compact code.
& #8226; the rational use of the keyword const can enable the compiler to naturally protect parameters that do not want to be changed and prevent them from being accidentally modified by code. In short, this can reduce the occurrence of bugs.
Volatile

8. What does the keyword volatile mean? Three different examples are provided.
A variable defined as volatile means that this variable may be unexpectedly changed, so that the compiler will not assume the value of this variable. Precisely, the optimizer must carefully re-read the value of this variable every time when using this variable, rather than using the backup stored in the register. The following are examples of volatile variables:
& #8226; hardware registers of parallel devices (for example, Status Registers)
& #8226; Non-automatic variables that will be accessed in an interrupt service subroutine)
& #8226; variables shared by several tasks in multi-threaded applications

People who cannot answer this question will not be hired. I think this is the most basic problem to distinguish between C programmers and embedded system programmers. Embedded people often deal with hardware, interruptions, RTOS, etc. All of these require volatile variables. If you do not know volatile content, it will lead to disasters.
If the subject correctly answers this question (well, I suspect it will be the case), I will go a little deeper to see if this guy understands the full importance of volatile.
& #8226; can a parameter be const or volatile? Explain why.
& #8226; can a pointer be volatile? Explain why.
& #8226; what are the following function errors:
Int square (volatile int * PTR)
{
Return * PTR ** PTR;
}

The answer is as follows:
& #8226; yes. One example is read-only status registers. It is volatile because it may be unexpectedly changed. It is const because the program should not try to modify it.
& #8226; yes. Although this is not very common. One example is when a service subroutine repairs a pointer to a buffer.
& #8226; this code is a little abnormal. The purpose of this Code is to return the pointer * PTR points to the square of the value. However, since * PTR points to a volatile parameter, the compiler will generate code similar to the following:

Int square (volatile int * PTR)
{
Int A, B;
A = * PTR;
B = * PTR;
Return a * B;
}

* The value of * PTR may be unexpectedly changed, so a and B may be different. As a result, this Code may not return the expected square value! The correct code is as follows:

Long square (volatile int * PTR)
{
Int;
A = * PTR;
Return a *;
}

Bit manipulation)

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.