Pencils up, everyone. Here's a test to identify potential embedded programmers or embedded programmers with potential
An obligatory and significant part of the recruitment process for embedded systems programmers ems to be the "C test." Over the years, I have had to both take and prepare such tests and, in doing so, have realized that the tests can be informative for both the interviewer and interviewee. Furthermore, when given outside the pressure of an interview situation, the tests can also be quite entertaining.
From the interviewee's perspective, you can learn a lot about the person who has written or administered the test. Is the test designed to show off the writer's knowledge of the minutiae of the ANSI standard rather than to test practical know-how? Does it test ludicrous knowledge, such as the ASCII values of certain characters? Are the questions heavily slanted towards your knowledge of system calls and memory allocation strategies, indicating that the writer may spend his time programming computers instead of embedded systems? If any of the are true, then I know I would riously doubt whether I want the job in question.
From the interviewer's perspective, a test can reveal veral things about the candidate. Primarily, you
can determine the level of the candidate's knowledge of C. However, it's also interesting to e how the person responds to questions to which they don't know the answers. Do they make intelligent choices backed up with good intuition, or do they just guess? Are they defensive when they are stumped, or do they exhibit a real curiosity about the problem and e it as an opportunity to learn something? I find this information as uful as their raw performance on the test.
With the ideas in mind, I have attempted to construct a test that is heavily slanted towards the requirements of embedded systems. This is a lousy test to give to someone eking a job writing compilers! The questions are almost all drawn from situations I have encountered over the years. Some of them are tough; however, they should all be informative.
This test may be given to a wide range of candidates. Most entry-level applicants will do poorly on this test, while asoned veterans should do very well. Points are not assigned to each question, as this tends to arbitrarily weight certain questions. However, if you choo to adapt this test for your own us, feel free to assign scores.
Preprocessor
1. Using the #define statement, how would you declare a manifest constant that returns the number
of conds in a year? Disregard leap years in your answer.
#define SECONDS_PER_YEAR (60 * 60 * 24 * 365)UL
I'm looking for veral things here:
? Basic knowledge of the #define syntax (for example, no mi-colon at the end, the need to parenthesize, and so on)
? An understanding that the pre-processor will evaluate constant expressions for you. Thus, it is clearer, and penalty-free, to spell out how you are calculating the n
umber of conds in a year, rather than actually doing the calculation yourlf
? A realization that the expression will overflow an integer argument on a 16-bit machine-hence the need for the L, telling the compiler to treat the variable as a Long
? As a bonus, if you modified the expression with a UL (indicating unsigned long), then you are off to a great start. And remember, first impressions count!
2. Write the "standard" MIN macro-that is, a macro that takes two arguments and returns the smaller of the two arguments.
#define MIN(A,B) ((A) <= (B) ? (A) : (B))
The purpo of this question is to test the following:
? Basic knowledge of the #define directive as ud in macros. This is important becau until the inline operator becomes part of standard C, macros are the only portable way of generating inline code. Inline code is often necessary in embedded systems in order to achieve the required performance level
? Knowledge of the ternary conditional operator. This operator exists in C becau it allows the compiler to produce more optimal code than an if-then-el quence. Given that performance is normally an issue in embedded systems, knowledge and u of this construct is important
? Understanding of the need to very carefully parenthesize arguments to macros
? I also u this question to start a discussion on the side effects of macros, for example, what happens when you write code such as:
least = MIN(*p++, b);
3. What is the purpo of the preprocessor directive #error?
Either you know the answer to this, or you don't. If you don't, e Reference 1. This question is uful for differentiating between normal folks and the nerds. Only the nerds actually read the appendices of C textbooks to find out about such things. Of cour, if you aren't looking for a nerd, the candidate better hope she doesn't know the answer.
Infinite loops
4. Infinite loops often ari in embedded systems. How does you code an infinite loop in C?
There are veral solutions to this question. My preferred solution is:
党员转正介绍人发言while(1)
{
?}
Many programmers em to prefer:
for(;;)
{
社会调查研究与方法?}
This construct puzzles me becau the syntax doesn't exactly spell out what's going on. Thus, if a candidate gives this as a solution, I'll u it as an opportunity to explore their rationale for doing so. If their answer is basically, "I was taught to do it this way and I haven't thought about it since," it tells me something (bad) about them.
元宵节小品
A third solution is to u a goto :
Loop:
...
goto Loop;
Candidates who propo this are either asmbly language programmers (which is probably good), or el they are clot BASIC/FORTRAN programmers looking to get into a new field.
Data declarations
5. Using the variable a, give definitions for the following:
a) An integer
b) A pointer to an integer
c) A pointer to a pointer to an integer
d) An array of 10 integers
e) An array of 10 pointers to integers
f) A pointer to an array of 1
0 integers
g) A pointer to a function that takes an integer as an argument and returns an integer
h) An array of ten pointers to functions that take an integer argument and return an integer
The answers are:
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 a couple of the are the sorts of thing that one looks up in textbooks-and I agree. While writing this article, I consulted textbooks to ensure the syntax was correct. However, I expect to be asked this question (or something clo to it) when I'm being interviewed. Conquently, I make sure I know the answers, at least for the few hours of the interview. Candidates who don't know all the answers (or at least most of them) are simply unprepared for the interview. If they can't be prepared for the interview, what will they be prepared for?
Static
6. What are the us of the keyword static?
This simple question is rarely answered completely. Static has three distinct us in C:
? A variable declared static within the body of a function maintains its value between function invocations
? A variable declared static within a module, (but outside the body of a function) is accessible by all functions within that module. It is not accessible by functions within any other module. That is, it is a localized global
?
Functions declared static within a module may only be called by other functions within that module. That is, the scope of the function is localized to the module within which it is declared 肩膀酸痛怎么缓解
Most candidates get the first part correct. A reasonable number get the cond part correct, while a pitiful number understand the third answer. This is a rious weakness in a candidate, since he obviously doesn't understand the importance and benefits of localizing the scope of both data and code.
Const
7. What does the keyword const mean?
As soon as the interviewee says "const means constant," I know I'm dealing with an amateur. Dan Saks has exhaustively covered const in the last year, such that every reader of ESP should be extremely familiar with what const can and cannot do for you. If you haven't been reading that column, suffice it to say that const means "read-only." Although this answer doesn't really do the subject justice, I'd accept it as a correct answer. (If you want the detailed answer, read Saks' columns-carefully!)
If the candidate gets the answer correct, I'll ask him the supplemental questions:
What do the following declarations mean?
const int a;
int const a;
const int *a;
int * cons
t a;
int const * a const;
养生壶怎么用
The first two mean the same thing, namely a is a const (read-only) integer. The third means a is a pointer to a const integer (that is, the integer isn't modifiable, but the pointer is). The fourth declares a to be a const pointer to an integer (that is, the integer pointed to by a is modifiable, but the pointer is not). The final declaration declares a to be a const pointer to a const integer (that is, neither the integer pointed to by a, nor the pointer itlf may be modified). If the candidate correctly answers the questions, I'll be impresd. Incidentally, you might wonder why I put so much emphasis on co
nst, since it is easy to write a correctly functioning program without ever using it. I have veral reasons:
? The u of const conveys some very uful information to someone reading your code. In effect, declaring a parameter const tells the ur about its intended usage. If you spend a lot of time cleaning up the mess left by other people, you'll quickly learn to appreciate this extra piece of information. (Of cour, programmers who u const , rarely leave a mess for others to clean up.)
? const has the potential for generating tighter code by giving the optimizer some additional information
? Code that us const liberally is inherently protected by the compiler against inadvertent coding constructs that result in parameters being changed that should not be. In short, they tend to have fewer bugs
Volatile
8. What does the keyword volatile mean? Give three different examples of its u.
A volatile variable is one that can change unexpectedly. Conquently, the compiler can make no as
sumptions about the value of the variable. In particular, the optimizer must be careful to reload the variable every time it is ud instead of holding a copy in a register. Examples of volatile variables are:
? Hardware registers in peripherals (for example, status registers)
? Non-automatic variables referenced within an interrupt rvice routine
? Variables shared by multiple tasks in a multi-threaded application
Candidates who don't know the answer to this question aren't hired. I consider this the most fundamental question that distinguishes between a C programmer and an embedded systems programmer. Embedded folks deal with hardware, interrupts, RTOSes, and the like. All of the require volatile variables. Failure to understand the concept of volatile will lead to disaster.
On the (dubious) assumption that the interviewee gets this question correct, I like to probe a little deeper to e if they really understand the full significance of volatile . In particular, I'll ask them the following additional questions:
? Can a parameter be both const and volatile ? Explain.
? Can a pointer be volatile ? Explain.
? What's wrong with the following function?:
int square(volatile int *ptr)
{
return *ptr * *ptr;
}
The answers are as follows:
? Yes. An example is a read-only status register. It i
s volatile becau it can change unexpectedly. It is const becau the program should not attempt to modify it
? Yes, although this is not very common. An example is when an interrupt rvice routine modifies a pointer to a buffer
? This one is wicked. The intent of the code is to return the square of the value pointed to by *ptr . However, since *ptr points to a volatile parameter, the compiler will generate code that looks something like this:
int square(volatile int *ptr)
{
int a,b;
a = *ptr;
舌加一笔b = *ptr;
测试题大全return a * b;
}
Becau it's possible for the value of *ptr to change unexpectedly, it is possible for a and b to be different. Conquently, this code could return a number that is not a square! The correct way to code this is:
long square(volatile int *ptr)
{
int a;
a = *ptr;
return a * a;
}
Bit manipulation
9. Embedded systems always require the ur to manipulate bits in registers or variables. Given an integer variable a, write two code fragments. The first should t bit 3 of a. The cond should clear bit 3 of a. In both cas, the remaining bits should be unmodified.
The are the three basic respons to this question:
? No idea. The interviewee cannot have done any embedded systems work
? U bit fields. Bit fields are right up there with trigraphs as the most brain-dead portion of C. Bit fields are inherently non-portable across compilers, and as such guarantee that your code is not reusable. I recently had the misfortune to look at a driver written by Infineon for one of their more complex communications chips. It ud bit fields and was completely uless becau my compiler implemented the bit fields the other way around. The moral: never let a non-embedded person anywhere near a real piece of hardware!
? U #defines and bit masks. This is a highly portable method and is the one that should be ud. My optimal solution to this problem would be:
#define BIT3 (0x1 << 3)
static int a;
void t_bit3(void) {
a |= BIT3;
}
void clear_bit3(void) {
a &= ~BIT3;
}
Some people prefer to define a mask together with manifest constants for the t and clear values. This is also acceptable. The element that I'm looking for is the u of manifest constants, together with the |= and &= ~ constructs
Accessing fixed memory locations
10. Embedded systems are often characterized by requiring the programmer to access a specific memory location. On a certain project it is required to t an integer variable at the absolute address 0x67a9 to the value 0xaa55. The compiler is a pure ANSI compiler. Write code to accomplish this task.
This problem tests whether you know that it is legal to typecast an integer to a pointer in order to access an absolute location. The exact syntax varies depending upon one's style. However, I would typically be looking for something like this:
int *ptr;
ptr = (int *)0x67a9;
*ptr = 0xaa55;
A more obscure approach is:
*(int * const)(0x67a9) = 0xaa55;
Even if yo