Beruflich Dokumente
Kultur Dokumente
NIGEL JONES
f e a t u r e
A ‘C’ Test: The 0x10
Best Questions for
Would-be Embedded
Programmers
Pencils up, everyone. Here’s a test to identify potential
embedded programmers or embedded programmers with potential
A
n obligatory and significant part of the seriously doubt whether I want the job in question.
recruitment process for embedded systems From the interviewer’s perspective, a test can reveal sev-
programmers seems to be the “C test.” Over eral things about the candidate. Primarily, you can deter-
the years, I have had to both take and pre- mine the level of the candidate’s knowledge of C. However,
pare such tests and, in doing so, have real- it’s also interesting to see how the person responds to ques-
ized that these tests can be informative for tions to which they don’t know the answers. Do they make
both the interviewer and interviewee. Furthermore, when intelligent choices backed up with good intuition, or do
given outside the pressure of an interview situation, these they just guess? Are they defensive when they are stumped,
tests can also be quite entertaining. or do they exhibit a real curiosity about the problem and see
From the interviewee’s perspective, you can learn a lot it as an opportunity to learn something? I find this informa-
about the person who has written or administered the test. tion as useful as their raw performance on the test.
Is the test designed to show off the writer’s knowledge of the With these ideas in mind, I have attempted to construct a
minutiae of the ANSI standard rather than to test practical test that is heavily slanted towards the requirements of embed-
know-how? Does it test ludicrous knowledge, such as the ded systems. This is a lousy test to give to someone seeking a
ASCII values of certain characters? Are the questions heavi- job writing compilers! The questions are almost all drawn
ly slanted towards your knowledge of system calls and mem- from situations I have encountered over the years. Some of
ory allocation strategies, indicating that the writer may them are tough; however, they should all be informative.
spend his time programming computers instead of embed- This test may be given to a wide range of candidates.
ded systems? If any of these are true, then I know I would Most entry-level applicants will do poorly on this test, while
Static
6. What are the uses of the keyword
static?
really do the subject justice, I’d accept to be a const pointer to a const integer mizer some additional information
it as a correct answer. (If you want the (that is, neither the integer pointed to • Code that uses const liberally is
detailed answer, read Saks’ columns— by a, nor the pointer itself may be mod- inherently protected by the compiler
carefully!) ified). If the candidate correctly answers against inadvertent coding con-
If the candidate gets the answer these questions, I’ll be impressed. structs that result in parameters
correct, I’ll ask him these supplemen- Incidentally, you might wonder why being changed that should not be.
tal questions: I put so much emphasis on const, In short, they tend to have fewer bugs
since it is easy to write a correctly func-
What do the following declarations mean? tioning program without ever using it. Volatile
const int a; I have several reasons: 8. What does the keyword volatile mean?
int const a; Give three different examples of its use.
const int *a; • The use of const conveys some very
int * const a; useful information to someone A volatile variable is one that can
int const * a const; reading your code. In effect, declar- change unexpectedly. Consequently,
ing a parameter const tells the user the compiler can make no assump-
The first two mean the same thing, about its intended usage. If you tions about the value of the variable.
namely a is a const (read-only) integer. spend a lot of time cleaning up the In particular, the optimizer must be
The third means a is a pointer to a mess left by other people, you’ll careful to reload the variable every
const integer (that is, the integer isn’t quickly learn to appreciate this extra time it is used instead of holding a
modifiable, but the pointer is). The piece of information. (Of course, copy in a register. Examples of
fourth declares a to be a const pointer programmers who use const, rarely volatile variables are:
to an integer (that is, the integer point- leave a mess for others to clean up.)
ed to by a is modifiable, but the pointer • const has the potential for generat- • Hardware registers in peripherals
is not). The final declaration declares a ing tighter code by giving the opti- (for example, status registers)
• Non-automatic variables refer- • Can a parameter be both const and the code is to return the square of
enced within an interrupt service volatile? Explain. the value pointed to by *ptr.
routine • Can a pointer be volatile? However, since *ptr points to a
• Variables shared by multiple tasks Explain. volatile parameter, the compiler
in a multi-threaded application • What’s wrong with the following will generate code that looks some-
function?: thing like this:
Candidates who don’t know the
answer to this question aren’t hired. I int square(volatile int *ptr) int square(volatile int *ptr)
consider this the most fundamental { {
question that distinguishes between a return *ptr * *ptr; int a,b;
C programmer and an embedded sys- } a = *ptr;
tems programmer. Embedded folks b = *ptr;
deal with hardware, interrupts, The answers are as follows: return a * b;
RTOSes, and the like. All of these }
require volatile variables. Failure to • Yes. An example is a read-only sta-
understand the concept of volatile tus register. It is volatile because it Because it’s possible for the value
will lead to disaster. can change unexpectedly. It is of *ptr to change unexpectedly, it is
On the (dubious) assumption that const because the program should possible for a and b to be different.
the interviewee gets this question cor- not attempt to modify it Consequently, this code could return a
rect, I like to probe a little deeper to • Yes, although this is not very com- number that is not a square! The cor-
see if they really understand the full mon. An example is when an inter- rect way to code this is:
significance of volatile. In particular, rupt service routine modifies a
I’ll ask them the following additional pointer to a buffer long square(volatile int *ptr)
questions: • This one is wicked. The intent of {
void set_bit3(void) {
a |= BIT3;
}
void clear_bit3(void) {
a &= ˜BIT3;
}
radius; printf() often has problems with ing signed and unsigned types have all
printf(“\nArea = %f”, area); reentrancy and performance. If you operands promoted to unsigned types.
return area; missed points three and four, I would- Thus –20 becomes a very large positive
} n’t be too hard on you. Needless to integer and the expression evaluates to
say, if you got these last two points, greater than 6. This is a very important
This function has so much wrong with your employment prospects are look- point in embedded systems where
it, it’s hard to know where to start: ing better and better unsigned data types should be used fre-
quently (see Reference 2). If you get this
• ISRs cannot return a value. If you Code examples one wrong, you are perilously close to
don’t understand this, you aren’t 12. What does the following code output not getting the job.
hired and why?
• ISRs cannot be passed parameters. 13. Comment on the following code frag-
See the first item for your employ- void foo(void) ment.
ment prospects if you missed this {
• On many processors/compilers, unsigned int a = 6; unsigned int zero = 0;
floating-point operations are not int b = -20; unsigned int compzero = 0xFFFF;
necessarily re-entrant. In some (a+b > 6) ? puts(“> 6”) : /*1’s complement of zero */
cases one needs to stack additional puts(“<= 6”);
registers. In other cases, one simply } On machines where an int is not 16
cannot do floating point in an ISR. bits, this will be incorrect. It should be
Furthermore, given that a general This question tests whether you under- coded:
rule of thumb is that ISRs should stand the integer promotion rules in C—
be short and sweet, one wonders an area that I find is very poorly under- unsigned int compzero = ˜0;
about the wisdom of doing floating- stood by many developers. Anyway, the
point math here answer is that this outputs “> 6.” The This question really gets to whether
• In a vein similar to the third point, reason for this is that expressions involv- the candidate understands the impor-
method, if any, is preferred and why? int a = 5, b = 7, c; coding styles, the value of code
This is a very subtle question, and any- c = a+++b; reviews, and the benefits of using lint.
one who gets it right (for the right rea- Well folks, there you have it. That
son) is to be congratulated or con- This question is intended to be a light- was my version of the C test. I hope you
demned (“get a life” springs to mind). hearted end to the quiz, as, believe it had as much fun taking it as I had writ-
The answer is the typedef is preferred. or not, this is perfectly legal syntax. ing it. If you think the test is a good test,
Consider the declarations: The question is how does the compil- then by all means use it in your recruit-
er treat it? Those poor compiler writ- ment. Who knows, I may get lucky in a
dPS p1,p2; ers actually debated this issue, and year or two and end up being on the
tPS p3,p4; came up with the “maximum munch” receiving end of my own work. esp
rule, which stipulates that the compil-
The first expands to: er should bite off as big (and legal) a Nigel Jones is a consultant living in
chunk as it can. Hence, this code is Maryland. When not underwater, he can
struct s * p1, p2; treated as: be found slaving away on a diverse range
of embedded projects. He enjoys hearing
which defines p1 to be a pointer to the c = a++ + b; from readers and can be reached at
structure and p2 to be an actual struc- NAJones@compuserve.com.
ture, which is probably not what you Thus, after this code is executed, a = 6,
wanted. The second example correctly b = 7, and c = 12. References
defines p3 and p4 to be pointers. If you knew the answer, or guessed Jones, Nigel, “In Praise of the #error direc-
correctly, well done. If you didn’t tive,” Embedded Systems Programming,
Obscure syntax know the answer then I wouldn’t con- September 1999, p. 114.
16. C allows some appalling constructs. Is sider this to be a problem. I find the Jones, Nigel, “Efficient C Code for Eight-
this construct legal, and if so what does this greatest benefit of this question is that bit MCUs,” Embedded Systems
code do? it is good for stimulating questions on Programming, November 1998, p. 66.