I translated a piece of article, please advise, if you like it, I will send the rest.
C
Language test: I want to be embedded programmers
0x10
Basic problem
source
http://www.embedded.com/2000/0005/0005feat2.htm
Nigel Jones
Translator:
RTOSFUN Email: rtosfun@gmail.com
Dear, take out a pencil. This test will tell if you have potential to become an embedded programmer, or you are not a potential embedded programmer
C
Language test is a method necessary and effective in the process of recruiting embedded system programmers. These years, I also participated in many of this test. In this process I realized that these tests can provide many useful information for the interviewer and the interviewer. In addition, the pressure of the interview does not talk, this test is also Quite interesting.
From the perspective of the interviewer, you can learn more about the topic or invigilator. This test is just a matter of question.
ANSI
Does the standard detail knowledge rather than technical secrets? Is this stupid question? To answer a character
ASCII
value. Do you focus on your system calls and memory allocation strategies? This marks that the topic may spend time on the microcomputer without being on the embedded system. If the answer to any of the above questions is
"
Yes
"
If I know that I have to consider whether I should do this job.
From the perspective of the interviewer, a test may reveal the quality of the respondents from many aspects: the most basic, you can understand the entry
C
Language level. Anyway, look at how this person answers the problem he will not be full. Is the candidate to make a wise choice with a good intuition, or is it just a shame? When the candidate is caught in a question, it is to find an excuse, or show the real curiosity of the problem, and see this as a chance to learn? I found this information as useful as their test score.
With these ideas, I decided some questions that truly target embedded systems, I hope that these headaches can help people who are looking for work. These issues are actually encountered in these years. Some of these questions are difficult, but they should give you a little enlightenment.
This test suitable for different levels, most primary levels of candidates are very poor, and experienced programmers should have good results. In order for you to decide some of the preferences, each problem does not assign a score. If you choose these exam questions, please share the score according to your meaning.
Preprocessor (
Preprocessor
)
1 .
Pretreatment instruction
#define
Declare a constant to indicate
1
How many seconds in the year (ignore the leap year problem)
#define seconds_per_year (60 * 60 * 24 * 365) UL
I want to see a few things here:
#define
The basic knowledge of grammar (for example: cannot end with semicolons, brackets, etc.)
Know that the preprocessor will calculate the value of the constant expression, so it is clearer without calculating the actual value, which is more clear and no price.
Realizing that this expression will make one
16
The integer number of bodies
-
So use long integer symbols
L,
Tell the compiler This constant is a long integer.
If you are used in your expression
UL
(Expressed no sign long), then you have a good start. Remember, the first impression is very important.
2 .
Write one
"
standard
"
Macro
MIN
This macro inputs two parameters and returns a smaller one.
#define min (a, b)
((A)
<
= (B)? (A): (b)) This test is set to the following purposes:
Logo
#define
Basic knowledge applied in macro. This is very important because until embedding
(inline)
Operational syndrome is standard
C
Part part, macro is convenient to generate a unique way to embedded code, for embedded systems, in order to achieve the required performance, embedded code is often a necessary method.
Knowledge of triple condition operators. This operator exists
C
The reason is why it makes the compiler to produce a ratio
IF-THEN-ELSE
More optimized code, understanding this usage is important.
I know how to enclose the parameters in the middle of the macro.
I also use this question to discuss the side effects of macros, for example: What happens when you write the following code?
LeaSt = min (* p , b);
3.
Preprocessor identification
#Error
What is the purpose?
If you don't know the answer, please see the reference
1
. This issue is useful for distinguishing a normal buddy and a nerd. Only nerd will read
C
The appendix of the language textbooks to find the answer to this problem. Of course, if you are not looking for a nerd, then the respondent is best to want to know the answer.
Dead cycle (
Infinite loops
)
4.
In-embedded systems often use infinite loops, how do you use
C
Write a dead cycle?
This problem uses several solutions. My preferred program is:
While (1)
{
?
Some programmers prefer the following scenario:
For (;;)
{
?
This implementation makes me difficult, because this syntax does not have exact expressions. If a candidate gives this as a program, I will use this as a chance to explore the basic principles they do. If their basic answers are:
"
I was taught to do this, but I never thought about why.
"
This will give me a bad impression.
The third solution is
goto
Loop
:
...
Goto loop;
If the subject gives the above scheme, this explanation or he is a compilation language programmer (this may be a good thing) or he is a new field.
Basic / Fortran
programmer.
Data declaration
Data Declarations
)
5.
Variable
a
Give the following definition
a)
A intensive number
An inTeger
)
b)
A pointer to the whole number (
A pointer to an integer
)
c)
A pointer to a pointer, which pointing to a pointer pointing to a constant number (
A Pointer to a pointer to an inTege
)
r
d)
A
10
An array of intensive numbers (
An Array Of 10 Integers
)
e)
A
10
An array of pointers that point to a integer. (
An Array of 10 Pointers to Integers
)
f)
A pointing
10
Integer number of pointers (
A pointer to an array of 10 integers
)
g)
A pointer to a function, which has a integer parameter and returns a constant number (
A Pointer to a function Takes an integer as an argument and returns an integer
)
h)
A
10
An array of pointers, the pointer points to a function, the function has an integer parameter and returns a constant number (
An Array of Ten Pointers to Functions 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 Takes an integer argument and returns an integer
h) int (* a [10]) (int); // AN array of 10 Pointers to functions what take an integer argument and return an integer
People often claim that there are several problems here that I have to turn back the book to answer questions, I agree with this statement. When I wrote this article, in order to determine the correctness of the grammar, I really checked a book. But when I was interviewed, I expected to be asked (or a similar problem). Because in this time of the interview, I am sure I know the answer to this question. If you don't know all the answers (or at least most of the answers), then you have not prepared for this interview. If the interviewer is not prepared for this interview, then he can prepare?
Static
6.
Keyword
Static
What is the role?
This simple problem is very fewer people answering complete. in
C
Language, keyword
Static
There are three obvious roles:
In a function body, a declared variable is static variable to maintain its value unchanged during this function.
In the module (but outside the function), a variable that is declared static can be accessed by the function used in the module, but cannot be accessed by other functions outside the module. It is a local global variable.
In the module, a function that is declared as a static function can only be called by other functions within this module. That is, this function is restricted to declare the local range of its module.
Most candidents can answer the first part correctly, and some can answer the second part correctly, and they can understand the third part. This is a serious disadvantage of a candidate because he obviously does not know the benefits and importance of localized data and code range.
Const
Seduce
. Keyword
Const
What is the meaning?
As long as I hear the interviewer:
"Const
Meant constant
"
I will know that I am in harmony with a honest. last year
Dan Saks
Already fully summarized in his article
Const
All usage, so
ESP
Translator:
Embedded Systems Programming)
Every reader should be very familiar
Const
What can I do and what can't be done?
.
If you have never read the article, just say it
Const
mean
"
Read-only
"
Yes. Although this answer is not a complete answer, I accept it as a correct answer. (If you want to know more detailed answers, read it carefully.
Saks
Article. )
If the candier can answer this question correctly, I will ask him an additional question:
What does the following statement mean? Const Int A;
Int const a;
Const Int * a;
INT * Const A;
Int const * a const;
The first two roles are the same,
a
Is a constant number. The third one means
a
It is a pointer to the constant number (that is, the integer is unmodified, but the pointer can be). Fourth meaning
a
It is a normally pointer to the whole number (that is, the integer of pointer points to modify, but the pointer is not modified). The last thing means
a
It is a normal pointer to the constant number (that is, the integer of the pointer is not modified, and the pointer is also unmodified). If the candidate can answer these questions correctly, he will leave me a good impression. In the way, maybe you may ask, even if you don't need keywords
Const
It is also easy to write the correct program, then I will see the keywords.
Const
? I also have the following reason:
Keyword
Const
The role is to give a very useful information for people who read your code, in fact, declare a parameter is to tell the user's application purpose. If you have spent a lot of time to clean up the garbage left by others, you will be soon thank you for this extra information ã. (Of course, know how to use
Const
The programmer rarely leaves the garbage to let others clean up. )
Use keywords by adding some additional information to the optimizer
Const
Maybe it can produce a more compact code.
Reasonably use keywords
Const
The compiler can naturally protect the parameters that do not want to be changed to prevent unintentional code modifications. In short, this can be reduced
Bug
The appearance.
Volatile
8.
Keyword
Volatile
What is the meaning?
?
And give three different examples.
One definition is
Volatile
The variable is that this variable may be unexpected. In this way, the compiler will ä ä to assume the value of this variable. Precisely said that the optimizer must reread the value of this variable every time when used this variable, rather than using the backup stored in the register. Below is
Volatile
Several examples of variables:
Parallel equipment hardware registers (such as status registers)
Non-automatic variables accessed in an interrupt service subroutine
(Non-automatic variables)
Variables shared by several tasks in multi-threaded applications
People who can't answer this question will not be hired. I think this is distinguishing
C
The most basic problem with programmers and embedded system programmers. Embedded system programmers often work with hardware, interruption,
RTOS
Wait, etc., these are required
Volatile
variable. Do not know
Volatile
The content will bring disasters.
Suppose it is an interviewer to answer this is a problem (um, doubt if this is this), I will have a little more, look at this guy, don't know how to understand
Volatile
Complete importance.
A parameter can be either
Const
OK
Volatile
? Explain why.
One pointer can be
Volatile
? Explain why.
What is wrong with the following functions:
INT Square (Volatile Int * PTR)
{
Return * ptr * * PTR;
}
Here is the answer:
Yes it is. An example is a read-only status register. it is
Volatile
Because it may be unexpectedly changed. it is
Const
Because the program should not try to modify it.
Yes it is. Although this is not very common. An example is when a medium service subroutine repairs a point to one
Buffer
The pointer is when it is.
This code has a prank. The purpose of this code is to return the pointer * Ptr
Pointing the square of the value, but due to
* Ptr
Point to one
Volatile
Type parameters, the compiler will produce the following code:
INT Square (Volatile Int * PTR)
{
INT A, B;
a = * PTR;
B = * PTR;
Return a * b;
}
due to
* Ptr
The value may be unexpectedly changed, so
a
with
b
It may be different. As a result, this code may return the square value you expect! The correct code is as follows:
Long Square (Volatile INT * PTR)
{
Int a;
a = * PTR;
Return A * a;
}
Bit operation
Bit manipulation
)
9.
The embedded system always wants to operate the variable or register. Give a set of integer variables
a
Write two pieces of code, the first setting
a
of
Bit 3
Second clear
a
of
Bit 3
. In the above two operations, keep other bits unchanged.
Three basic reactions to this issue
I don't know how to start. The subject has never done any embedded system.
use
Bit Fields
.
Bit Fields
Be thrown
C
Things of the language dead angle, it guarantees that your code is unmiglable between different compilers, and it is also guaranteed that your code is unused. I have never seen it recently.
INFINEON
Drivers written for its more complex communication chip, it is used
Bit Fields
So completely useless, because my compiler is implemented in other ways
Bit Fields
of. From moral speaking: Never let a non-embedded guy stick the actual hardware.
use
#defines
with
Bit masks
operating. This is a method with extremely high portability and should be used. The best solution is as follows:
#define bit3 (0x1 << 3)
Static Int A;
Void set_bit3 (void) {
a | = bit3;
}
Void clear_bit3 (void) {
A & = ~ bit3;
}
Some people like to define a mask simultaneously for setting and clear values, which is also acceptable. I hope to see a few points: explain the constant,
| =
with
& = ~
operating.
Access the fixed memory location (
Accessing fixed memory location
)
10.
Embedded systems often have the characteristics of requesting programmers to access a particular memory location. In a project, it is required to set an absolute address as
0x67a9
The value of the integer variable is
0xAA66
. Compiler is a pure
ANSI
translater. Write code to complete this task.
This issue tests if you know that in order to access an absolute address, a whole number is forced (
Typecast
) Is legal for a pointer. The implementation of this problem is different from the personal style. A typical similar code is as follows:
INT * PTR;
PTR = (int *) 0x67a9;
* PTR = 0xAA55;
A more obscure approach is:
A more embarrassing method is:
* (int * const) (0x67a9) = 0xAA55;
Even if your taste is closer to the second solution, I suggest you use the first solution during the interview.
Interruption
Interrupts
)
11.
Interrupt is an important part of the embedded system, which has led to many compiled developers to provide an extension.
-
Standard
C
Support interrupt. It is a representative fact that it produces a new keyword __interrupt
. The following code is used.
__interrupt
Keywords define an interrupt service subroutine
(ISR)
Please comment on this code.
__interrupt Double Compute_Area (Double Radius)
{
Double Area = Pi * Radius * Radius;
Printf ("/ Narea =% f", Area;
Return Area;
}
This function has too much mistake, so that people don't know why:
ISR
Can't return a value. If you don't understand this, then you will not be hired.
ISR
Unable to pass the parameters. If you don't see this, you are hired and equivalent to the first item.
In many processors
/
In the compiler, floating points are generally unreportable. Some processors
/
The compiler needs to get the amount of the register in the stack, some processors
/
The compiler is not allowed
ISR
Made floating point operations. In addition,
ISR
Should be short and efficient, in
ISR
It is unwise to do floating point operations.
Search with the third point of the pulse,
Printf ()
There is often a problem of re-entry and performance. If you lose your third and fourth points, I will not be too hard to be. Needless to say, if you can get two points, then your employment prospect is getting better and bright.
Code example (
Code Examples
)
12.
What is the following code output, why?
Void foo (void)
{
Unsigned int a = 6;
INT b = -20;
(A B> 6)? PUTS ("> 6"): PUTS ("<= 6");
}
This problem tests if you know
C
The integer automatic transformation principle in the language, I found some developers to understand these things. Anyway, the answer to this unsigned integer problem is that the output is
"> 6"
. The reason is that all operands are automatically converted to non-symbol type when there is a symbol type and unsigned type.
therefore
-20
It became a very large positive integer, so the result of this expression is greater than
6
. This is usually important for embedded systems that should be frequently used in unsigned data types. If you answer this question, you will not get the edge of this job.
13.
Evaluate the following code segment:
Unsigned int zero = 0;
Unsigned int compzero = 0xfff;
/ * 1'S Complement of Zero * /
A
int
Type is not
16
The bit processor is said that the above code is incorrect. Should be written as follows:
Unsigned int compzero = ~ 0;
This problem really exposes whether the candidate knows how to process the leader. In my experience, a good embedded programmer understands the hardware details and its limitations, however
PC
The machine often uses hardware as an unavoidable trouble.
At this stage, the respondent or completely dreamed or is full of confidence. If it is obvious that the test is not very good, then this test is over here. But if it is clear that the respondents are doing well, then I will throw the following additional problems, these problems are more difficult, I want to be very excellent at the advice. I would like to see how the respondents cope with the problem, not the answer. No matter what, you will be this entertainment.
...
Dynamic memory allocation
Dynamic Memory Allocation
)
14.
Although unlike non-embedded computers, embedded systems are still from piles (HEAP)
The process of allocating the memory in the middle. Then, in the embedded system, what is the problem that the dynamically allocated memory may happen?
Here, I expect that the candidate can mention the problem of memory fragmentation, debris collection, and the time of the variable. This topic is already
ESP
The magazine is widely discussed extensively (mainly
P.j. Plauger,
His explanation far exceeds any explanation that I can mention here), all of you look back these magazines! Let the respondents enter a false safe feeling, I took this small program:
What is the output of the code snippet below, why?
Char * PTR;
IF ((PTR = (char *) malloc (0)) ==
NULL)
Else
PUTS ("Got A Null Pointer";
PUTS ("Got a Valid Pointer");
This is an interesting question. Recently, a colleague in my colleague is inadvertently
0
The value is passed to the function
Malloc
After getting a legitimate pointer, I think of this problem. This is the above code, the output of this code is
"Got a Valid Pointer"
. I use this to start discussing such a problem to see if the interviewer thinks that the library routine is correct. Getting the right answer is important, but the method of solving the problem and the basic principles you make decisions are more important.
Typedef
:
15 Typedef
in
C
The language is frequently used to declare a synonym of a data type already existing. You can also do a similar thing with a preprocessor. For example, think about the following example:
#define DPS Struct S *
Typedef struct s * tps;
The intent of the above two cases is to be defined
DPS
with
TPS
As a pointing structure
s
pointer. Which method is better? (If any, why?
This is a very subtle problem, anyone answers to this issue (just reason) should be congratulated. the answer is:
Typedef
better. Think about the example below:
DPS P1, P2;
TPS P3, P4;
The first extension is
Struct S * P1, P2;
.
The above code definition
p1
For a pointing structure,
P2
For an actual structure, this may not be what you want. The second example is correctly defined.
P3
with
P4
Two pointers.
涩 涩 语
16. c
Language agrees some shocking structure
,
Is the following structure legally, if it is what it do?
INT A = 5, B = 7, C;
C = a b;
This issue will be a pleasant end of this test. No matter what you don't believe, the above example is completely syntax. The problem is how the compiler handles it? The compiling author of the level will actually argue this issue. According to the most processed principles, the compiler should be able to handle all legal uses as possible. Therefore, the above code is processed:
C = a b;
therefore
,
This code is behind
A = 6, b = 7, c = 12
.
If you know the answer, or guess the correct answer, do well. If you don't know the answer, I don't take this as a problem. I found the biggest benefit of this problem is
:
This is a good topic about code writing style, code readability, code modality.
Ok, guys, you have already finished all tests. This is my out.
C
Language test questions, I wrote it with a pleasant mood, I hope you have finished reading it with the same mood. If you think this is a good test, then try to use your job in the process of finding your work. God knows maybe for a year or two, I don't do now, and I need to find one. Nigel Jones
Is a consultant, now live
Maryland
When he is not underwater, you can find him in a plurality of embedded projects.
He is very happy to receive the reader's letter, his
address is
: Najones@compuserve.com
.
References
Jones, Nigel, "In Praise of The #Error Directive," Embedded Systems Programming, September 1999, P. 114.
Jones, Nigel, "Efficient C C code for Eight-Bit MCUS," Embedded Systems Programming, November 1998, P. 66.