Interview Guide for Embedded C Programmers

Accessing fixed memory locations

11. Embedded systems are often characterized by requiring the programmer to access a specific memory location. On a certain project it is required to set an integer variable at 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(ox67a9) = 0xaa55;

Even if your taste runs more to the second solution, we suggest the first solution when you are in an interview situation.

Free Course Signup

Interrupt

12. Interrupt are an important part of embedded systems. Consequently, many compiler vendors offer an extension to standard C to support interrupts. Typically, this new keyword is __interrupt. The following code uses __interrupt to define an interrupt service routine (ISR). Comment on the code.

__interrupt double compute_area(double radius)
{
    double area = PI * radius * radius;
    printf(“\n Area = %f”, area);
    return area;
}

This function has so much wrong with it, it’s hard to know where to start:

  • ISR cannot return a value. If you don’t understand this, you aren’t hired
  • ISR cannot be passed parameters. See the first item for your employment prospects if you missed this
  • On many processors or compilers, floating-point operations are not necessarily re-entrant. In some cases one needs to stack additional registers. In other cases, one simply cannot do floating point in an ISR. Furthermore, given that a general rule of thumb is that ISRs should be short and sweet, one wonders about the wisdom of doing floating-point math here
  • In a vein similar to the third point, printf() often has problems with reentrancy and performance. If you missed point three and four, anyone would be too hard on you. Needless to say, If you got this last two points, your employment prospect looking better and better.

Code Examples

13. What does the following code output and why?

void foo(void)
{
  unsigned int a = 6;
  int b = -20;
  (a+b>6) ? puts(“>6”): puts(“<=6”);
}

This question tests whether you understand the integer promotion in C-an area that I find is very poorly understood by many developers. Anyway, the answer is that this output “>6”. The reason for this is that expression involving signed and unsigned types has all operands promoted to unsigned types. Thus comes a very large positive integer and the expression evaluates to greater than 6. This is very important point in embedded systems where unsigned data types should be used frequently. If you get this wrong, you get this one wrong, you are perilously close to not getting the job.

14. Comment on the following code fragment.

unsigned int zero = 0;
unsigned int compzero = 0xFFFF;    /* 1’s complement of zero */

On machines where an int is not 16-bits, this will be incorrect. It should be coded:

unsigned int compzero = ~0;

This question really gets to whether the candidate understands the importance of word length on a computer. Its been seen that, good embedded programmers are critically aware of the underlying hardware and its limitations, whereas computer programmers tend to dismiss the hardware as a necessary annoyance.

By this stage, candidates are either completely demoralized-or they are on a roll and having a good time. If it’s obvious that the candidate isn’t very good, then the test is terminated at this point. However, if the candidate is doing well, then one may throw in these supplemental questions. These questions are hard, and expect that only the very best candidate will do well on them. In posing these questions, we often looking more at the way the candidate tackles the problems, rather than the answers. Anyway, have a fun.

Dynamic Memory Allocation

15. Although not as common as in non-embedded computers, embedded systems do still dynamically allocate memory from the heap. What are the problems with dynamic memory allocation in embedded systems?

Here, most expect the user to mentioned memory fragmentation, problem with garbage collection, variable execution time, and so on. This topic has been covered extensively in ESP, mainly by P.J. Plauger. His explanation is far more insightful than anything We could offer here, so go and read those back issues! Having lulled the candidate into a sense of false security, then offer up this tidbit:

What does the following code fragment output and why?

char *ptr;
if((ptr = (char *)malloc(0)) == NULL)
else
puts(“Got a null pointer”);
puts(“Got a valid pointer”);

This is a fun question. I stumbled across this only recently when a colleague of mine inadvertently passed a value of 0 to malloc and got back a valid pointer! That is, the above code will output “Got a valid pointer”. I use this to start a discussion on whether the interviewee thinks this is the correct thing for the library routine to do. Getting the right answer here is not nearly important as the way you approach the problem and the rationale for your decision.

Typedef

16. Typedef is frequently used in C to declare synonyms for pre-existing data types. It is also possible to use the preprocessor to do something similar. For instance, consider the following code fragment:

#define dPS struct s *
Typedef struct s * tPS;

The intent in both cases is to define dPS and tPS to be pointers to structure s. which method, if any, is preferred and why?

This is a very subtle question, and anyone who gets it right (for the right reason) is to be congratulated or condemned (“get a life” spring to mind). The answer is the typedef is preferred. Consider the declaration:

dPS p1,p2;
tPS p3,p4;

The first expands to:

struct s * p1,p2;

Which defines P1 to be a pointer to the structure and P2 to be an actual structure, which is probably not what you wanted. The second example correctly defines P3 and P4 to be pointer.

Obscure syntax

17. C allows some appalling constructs. Is this construct legal, and if so what does this code do?

int a = 5, b = 7, c;
c = a+++b;

This question is intended to be a lighthearted end to the quiz, as, believe it or not, this is perfectly legal syntax. The question is how does the compiler treat it? Those poor compiler writers actually debated this issue, and came up with the “maximum munch” rule, which stipulates that the compiler should bite off as big (and legal) a chunk as it can. Hence, this code is treated as:

c = a++ +b;

Thus, after this code is executed, a = 6, b = 7, c = 12

If you knew the answer, or guessed correctly, well done. If you didn’t know the answer then I wouldn’t consider this to be a problem. I find the greatest benefit of this question is that it is good for stimulating questions on coding styles, the value of code reviews, and the benefits of using lint.

That’s it for this article, I hope you will find this Interview Guide for Embedded C Programmers useful for your next job interview. I believe this set of questionnaire will help you to quickly revise concept before embedded software job interview.

Get Free Courses & Webinars
You'll receive only high quality learning material, tips & tricks
I agree to have my personal information transfered to MailChimp ( more information )
We respect your privacy

About Umesh Lokhande

Umesh Lokhande holds a Master degree in Scientific Instrumentation from University of Applied Sciences Jena, Germany. and has previously worked at Orbotech, Alere Technologies etc. Umesh is also a founder and first author of BINARYUPDATES.COM

Login

Register | Lost your password?