You are on page 1of 11

Best Questions for Embedded Programmers

Interview Questions: 1. What are static variables? 2. What are volatile variables? 3. What do you mean by const keyword ? 4. What is interrupt latency? 5. How you can optimize it? . What is size o! character" inte#er" inte#er pointer" character pointer? $. What is %&'' pointer and what is its use? (. What is void pointer and what is its use? ). What is *+,? 1-.What is return type o! *+,? 11..an we use any !unction inside *+,? 12..an we use print! inside *+,? 13..an we put breakpoint inside *+,? 14.How to decide whether #iven processor is usin# little endian !ormat or bi# endian !ormat ? 15.What is /op hal! 0 bottom hal! o! a kernel? 1 .1i!!erence between ,*+. and .*+. processor. 1$.What is ,/2+? 1(.What is the di!!erence between hard real3time and so!t real3time 2+? 1).What type o! schedulin# is there in ,/2+? 2-.What is priority inversion? 21.What is priority inheritance? 22.How many types o! *4. mechanism you know? 23.What is semaphore? 24.What is spin lock? 25.What is di!!erence between binary semaphore and mute5? 2 .What is virtual memory? 2$.What is kernel pa#in#? 2(..an structures be passed to the !unctions by value? 2).Why cannot arrays be passed by values to !unctions? 3-.6dvanta#es and disadvanta#es o! usin# macro and inline !unctions? 31.What happens when recursion !unctions are declared inline? 32.7de!ine cat85"y9 577y concatenates 5 to y. :ut cat8cat81"29"39 does not e5pand but #ives preprocessor warnin#. Why? 33..an you have constant volatile variable? ;es" you can have a volatile pointer? 34.<<=ip increments what? it increments what ip points to 35.2perations involvin# unsi#ned and si#ned > unsi#ned will be converted to si#ned 3 .malloc8sizeo!8-99 will return > valid pointer 3$.main89 ?!ork89@!ork89@!ork89@print!8Ahello worldA9@ B > will print ( times. 3(.6rray o! pts to !unctions > void 8=!ptrC1-D989 3).Which way o! writin# in!inite loops is more e!!icient than others? there are 3ways. +anEay 6huEa 1

4-.Who to know whether system uses bi# endian or little endian !ormat and how to convert amon# them? 41.What is !orward re!erence w.r.t. pointers in c? 42.How is #eneric list manipulation !unction written which accepts elements o! any kind? 43.What is the di!!erence between embedded systems and the system in which ,/2+ is runnin#? 44.How can you de!ine a structure with bit !ield members? 45.How do you write a !unction which takes 2 ar#uments 3 a byte and a !ield in the byte and returns the value o! the !ield in that byte? 4 .Which parameters decide the size o! data type !or a processor ? 4$.What is Eob o! preprocessor" compiler" assembler and linker ? 4(.What is the di!!erence between static linkin# and dynamic linkin# ? 4).How to implement a W1 timer in so!tware ?

Answers (by Nigel Jones) to some of the Pre"ro#essor

uestions!

$! Using the #define statement, how would you declare a manifest constant that returns the number of seconds in a year? Disregard leap years in your answer. %define &E'(N)&*PE+*,EA+ (-. / -. / 01 / 2-3)45 The purpose of this question is to test the following: Basic knowledge of the #define syntax (for example, no semi-colon at the end, the need to parenthesize, and so on !n understanding that the pre-processor will e"aluate constant expressions for you# Thus, it is clearer, and penalty-free, to spell out how you are calculating the num$er of seconds in a year, rather than actually doing the calculation yourself ! realization that the expression will o"erflow an integer argument on a %&-$it machinehence the need for the ', telling the compiler to treat the "aria$le as a 'ong !s a $onus, if you modified the expression with a (' (indicating unsigned long , then you are off to a great start# !nd remem$er, first impressions count)

0! Write the "standard" MI macro!that is, a macro that ta"es two arguments and returns the smaller of the two arguments. %define 6IN(A7B) ((A) 8 9 (B) : (A) : (B))

The purpose of this question is to test the following:

Basic knowledge of the #define directi"e as used in macros# This is important $ecause

+anEay 6huEa

until the inline operator $ecomes part of standard *, macros are the only porta$le way of generating inline code# +nline code is often necessary in em$edded systems in order to achie"e the required performance le"el ,nowledge of the ternary conditional operator# This operator exists in * $ecause it allows the compiler to produce more optimal code than an if-then-else sequence# -i"en that performance is normally an issue in em$edded systems, knowledge and use of this construct is important (nderstanding of the need to "ery carefully parenthesize arguments to macros + also use this question to start a discussion on the side effects of macros, for example, what happens when you write code such as: least 9 6IN(/";;7 b)<

2! What is the purpose of the preprocessor directi#e #error? .ither you know the answer to this, or you don/t# +f you don/t, see 0eference %# This question is useful for differentiating $etween normal folks and the nerds# 1nly the nerds actually read the appendices of * text$ooks to find out a$out such things# 1f course, if you aren/t looking for a nerd, the candidate $etter hope she doesn/t know the answer#

Infinite loo"s
1! Infinite loops often arise in embedded systems. $ow does you code an infinite loop in %? There are se"eral solutions to this question# 2y preferred solution is:

while($) = >

2any programmers seem to prefer:

for(<<) = >

This construct puzzles me $ecause the syntax doesn/t exactly spell out what/s going on# Thus, if a candidate gi"es this as a solution, +/ll use it as an opportunity to explore their rationale for doing so# +f their answer is $asically, 3+ was taught to do it this way and + ha"en/t thought a$out it since,3 it tells me something ($ad a$out them# ! third solution is to use a goto :

5oo":

+anEay 6huEa

!!! goto 5oo"<

*andidates who propose this are either assem$ly language programmers (which is pro$a$ly good , or else they are closet B!4+*5610T0!7 programmers looking to get into a new field#

)ata de#larations
3! Using the #ariable a, gi#e definitions for the following& a' (n integer b' ( pointer to an integer c' ( pointer to a pointer to an integer d' (n array of )* integers e' (n array of )* pointers to integers f' ( pointer to an array of )* integers g' ( pointer to a function that ta"es an integer as an argument and returns an integer h' (n array of ten pointers to functions that ta"e an integer argument and return an integer ?he answers are: a) int a< @@ An integer b) int /a< @@ A "ointer to an integer #) int //a< @@ A "ointer to a "ointer to an integer d) int aA$.B< @@ An array of $. integers e) int /aA$.B< @@ An array of $. "ointers to integers f) int (/a)A$.B< @@ A "ointer to an array of $. integers g) int (/a)(int)< @@ A "ointer to a fun#tion a that taCes an integer argument and returns an integer h) int (/aA$.B)(int)< @@ An array of $. "ointers to fun#tions that taCe an integer argument and return an integer 8eople often claim that a couple of these are the sorts of thing that one looks up in text$ooksand + agree# 9hile writing this article, + consulted text$ooks to ensure the syntax was correct# :owe"er, + expect to $e asked this question (or something close to it when +/m $eing inter"iewed# *onsequently, + make sure + know the answers, at least for the few hours of the inter"iew# *andidates who don/t know all the answers (or at least most of them are simply unprepared for the inter"iew# +f they can/t $e prepared for the inter"iew, what will they $e prepared for;

&tati#
-! What are the uses of the "eyword static? This simple question is rarely answered completely# 4tatic has three distinct uses in *: ! "aria$le declared static within the $ody of a function maintains its "alue $etween function in"ocations ! "aria$le declared static within a module, ($ut outside the $ody of a function is accessi$le $y all functions within that module# +t is not accessi$le $y functions within any other module# That is, it is a localized glo$al

+anEay 6huEa

6unctions declared static within a module may only $e called $y other functions within that module# That is, the scope of the function is localized to the module within which it is declared

2ost candidates get the first part correct# ! reasona$le num$er get the second part correct, while a pitiful num$er understand the third answer# This is a serious weakness in a candidate, since he o$"iously doesn/t understand the importance and $enefits of localizing the scope of $oth data and code#

'onst
D! What does the "eyword const mean? !s soon as the inter"iewee says 3const means constant,3 + know +/m dealing with an amateur# <an 4aks has exhausti"ely co"ered const in the last year, such that e"ery reader of .48 should $e extremely familiar with what const can and cannot do for you# +f you ha"en/t $een reading that column, suffice it to say that const means 3read-only#3 !lthough this answer doesn/t really do the su$=ect =ustice, +/d accept it as a correct answer# (+f you want the detailed answer, read 4aks/ columns-carefully) +f the candidate gets the answer correct, +/ll ask him these supplemental questions: What do the following declarations mean?

#onst int a< int #onst a< #onst int /a< int / #onst a< int #onst / a #onst<

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 modifia$le, $ut the pointer is # The fourth declares a to $e a const pointer to an integer (that is, the integer pointed to $y a is modifia$le, $ut the pointer is not # The final declaration declares a to $e a const pointer to a const integer (that is, neither the integer pointed to $y a, nor the pointer itself may $e modified # +f the candidate correctly answers these questions, +/ll $e impressed# +ncidentally, you might wonder why + put so much emphasis on const, since it is easy to write a correctly functioning program without e"er using it# + ha"e se"eral reasons: The use of const con"eys some "ery useful information to someone reading your code# +n effect, declaring a parameter const tells the user a$out its intended usage# +f you spend a lot of time cleaning up the mess left $y other people, you/ll quickly learn to appreciate this extra piece of information# (1f course, programmers who use const , rarely lea"e a mess for others to clean up# const has the potential for generating tighter code $y gi"ing the optimizer some additional information *ode that uses const li$erally is inherently protected $y the compiler against inad"ertent coding constructs that result in parameters $eing changed that should not $e# +n short,

+anEay 6huEa

they tend to ha"e fewer $ugs

Eolatile
F! What does the "eyword #olatile mean? +i#e three different e,amples of its use. ! "olatile "aria$le is one that can change unexpectedly# *onsequently, the compiler can make no assumptions a$out the "alue of the "aria$le# +n particular, the optimizer must $e careful to reload the "aria$le e"ery time it is used instead of holding a copy in a register# .xamples of "olatile "aria$les are:

:ardware registers in peripherals (for example, status registers 7on-automatic "aria$les referenced within an interrupt ser"ice routine >aria$les shared $y multiple tasks in a multi-threaded application

*andidates who don/t know the answer to this question aren/t hired# + consider this the most fundamental question that distinguishes $etween a * programmer and an em$edded systems programmer# .m$edded folks deal with hardware, interrupts, 0T14es, and the like# !ll of these require "olatile "aria$les# 6ailure to understand the concept of "olatile will lead to disaster# 1n the (du$ious assumption that the inter"iewee gets this question correct, + like to pro$e a little deeper to see if they really understand the full significance of "olatile # +n particular, +/ll ask them the following additional questions:

*an a parameter $e $oth const and "olatile ; .xplain# *an a pointer $e "olatile ; .xplain# 9hat/s wrong with the following function;:

int s uare(volatile int /"tr) = return /"tr / /"tr< >

The answers are as follows: ?es# !n example is a read-only status register# +t is "olatile $ecause it can change unexpectedly# +t is const $ecause the program should not attempt to modify it ?es, although this is not "ery common# !n example is when an interrupt ser"ice routine modifies a pointer to a $uffer This one is wicked# The intent of the code is to return the square of the "alue pointed to $y @ptr # :owe"er, since @ptr points to a "olatile parameter, the compiler will generate code that looks something like this:

int s uare(volatile int /"tr) = int a7b< a 9 /"tr< b 9 /"tr< return a / b<

+anEay 6huEa

>

Because it/s possi$le for the "alue of @ptr to change unexpectedly, it is possi$le for a and $ to $e different# *onsequently, this code could return a num$er that is not a square) The correct way to code this is:

long s uare(volatile int /"tr) = int a< a 9 /"tr< return a / a< >

Bit mani"ulation
G! -mbedded systems always re.uire the user to manipulate bits in registers or #ariables. +i#en an integer #ariable a, write two code fragments. /he first should set bit 0 of a. /he second should clear bit 0 of a. In both cases, the remaining bits should be unmodified. These are the three $asic responses to this question: 7o idea# The inter"iewee cannot ha"e done any em$edded systems work (se $it fields# Bit fields are right up there with trigraphs as the most $rain-dead portion of *# Bit fields are inherently non-porta$le across compilers, and as such guarantee that your code is not reusa$le# + recently had the misfortune to look at a dri"er written $y +nfineon for one of their more complex communications chips# +t used $it fields and was completely useless $ecause my compiler implemented the $it fields the other way around# The moral: ne"er let a non-em$edded person anywhere near a real piece of hardware) (se #defines and $it masks# This is a highly porta$le method and is the one that should $e used# 2y optimal solution to this pro$lem would $e:

%define BI?2 8 8 2) stati# int a<

(.H$

void set*bit2(void) = a I9 BI?2< > void #lear*bit2(void) = a J9 KBI?2< >

+anEay 6huEa

4ome people prefer to define a mask together with manifest constants for the set and clear "alues# This is also accepta$le# The element that +/m looking for is the use of manifest constants, together with the AB and CB D constructs

A##essing fiHed memory lo#ations


$.! -mbedded systems are often characteri1ed by re.uiring the programmer to access a specific memory location. 2n a certain pro3ect it is re.uired to set an integer #ariable at the absolute address *,45a6 to the #alue *,aa77. /he compiler is a pure ( 8I compiler. Write code to accomplish this tas". This pro$lem tests whether you know that it is legal to typecast an integer to a pointer in order to access an a$solute location# The exact syntax "aries depending upon one/s style# :owe"er, + would typically $e looking for something like this:

int /"tr< "tr 9 (int /).H-DaG< /"tr 9 .Haa33<

! more o$scure approach is:

/(int / #onst)(.H-DaG) 9 .Haa33<

."en if your taste runs more to the second solution, + suggest the first solution when you are in an inter"iew situation#

Interru"ts
$$! Interrupts are an important part of embedded systems. %onse.uently, many compiler #endors offer an e,tension to standard % to support interrupts. /ypically, this new "eyword is 99interrupt. /he following code uses 99interrupt to define an interrupt ser#ice routine :I8;'. %omment on the code.

**interru"t double #om"ute*area (double radius) = double area 9 PI / radius / radius< "rintf(LMnArea 9 NfL7 area)< return area< >

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

+anEay 6huEa

+40s cannot return a "alue# +f you don/t understand this, you aren/t hired +40s cannot $e passed parameters# 4ee the first item for your employment prospects if you missed this 1n many processors5compilers, floating-point operations are not necessarily re-entrant# +n some cases one needs to stack additional registers# +n other cases, one simply cannot do floating point in an +40# 6urthermore, gi"en that a general rule of thum$ is that +40s should $e short and sweet, one wonders a$out the wisdom of doing floating-point math here +n a "ein similar to the third point, printf( often has pro$lems with reentrancy and performance# +f you missed points three and four, + wouldn/t $e too hard on you# 7eedless to say, if you got these last two points, your employment prospects are looking $etter and $etter

'ode eHam"les
$0! What does the following code output and why?

void foo(void) = unsigned int a 9 -< int b 9 O0.< (a;b P -) : "uts(LP -L) : "uts(L 8 9 -L)< >

This question tests whether you understand the integer promotion rules in *-an area that + find is "ery poorly understood $y many de"elopers# !nyway, the answer is that this outputs 3E &#3 The reason for this is that expressions in"ol"ing signed and unsigned types ha"e all operands promoted to unsigned types# Thus Fcomes a "ery large positi"e integer and the expression e"aluates to greater than &# This is a "ery important point in em$edded systems where unsigned data types should $e used frequently (see 0eference G # +f you get this one wrong, you are perilously close to not getting the =o$# $2! %omment on the following code fragment.

unsigned int Qero 9 .< unsigned int #om"Qero 9 .HRRRR< @/$Ss #om"lement of Qero /@

1n machines where an int is not %& $its, this will $e incorrect# +t should $e coded:

unsigned int #om"Qero 9 K.<

This question really gets to whether the candidate understands the importance of word length on

+anEay 6huEa

a computer# +n my experience, good em$edded 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/re on a roll and ha"ing a good time# +f it/s o$"ious that the candidate isn/t "ery good, then the test is terminated at this point# :owe"er, if the candidate is doing well, then + throw in these supplemental questions# These questions are hard, and + expect that only the "ery $est candidates will do well on them# +n posing these questions, +/m looking more at the way the candidate tackles the pro$lems, rather than the answers# !nyway, ha"e fun###

)ynami# memory allo#ation


$1! (lthough 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? :ere, + expect the user to mention memory fragmentation, pro$lems with gar$age collection, "aria$le execution time, and so on# This topic has $een co"ered extensi"ely in ESP , mainly $y 8#H# 8lauger# :is explanations are far more insightful than anything + could offer here, so go and read those $ack issues) :a"ing lulled the candidate into a sense of false security, + then offer up this tid$it: What does the following code fragment output and why?

#har /"tr< if (("tr 9 (#har /)mallo#(.)) 99 N455) else "uts(LTot a null "ointerL)< "uts(LTot a valid "ointerL)<

This is a fun question# + stum$led across this only recently when a colleague of mine inad"ertently passed a "alue of I to malloc and got $ack a "alid pointer) That is, the a$o"e code will output 3-ot a "alid pointer#3 + use this to start a discussion on whether the inter"iewee thinks this is the correct thing for the li$rary routine to do# -etting the right answer here is not nearly as important as the way you approach the pro$lem and the rationale for your decision#

?y"edef
$3! ?y"edef is fre.uently used in % to declare synonyms for pre!e,isting data types. It is also possible to use the preprocessor to do something similar. <or instance, consider the following code fragment&

%define dP& stru#t s / ty"edef stru#t s / tP&<

/he intent in both cases is to define dP& and tP& to be pointers to structure s. Which method, if any, is preferred and why?

+anEay 6huEa

1-

This is a "ery su$tle question, and anyone who gets it right (for the right reason is to $e congratulated or condemned (3get a life3 springs to mind # The answer is the typedef is preferred# *onsider the declarations:

dP& "$7"0< tP& "27"1<

The first expands to:

stru#t s / "$7 "0<

which defines p% to $e a pointer to the structure and pG to $e an actual structure, which is pro$a$ly not what you wanted# The second example correctly defines pJ and pK to $e pointers#

(bs#ure syntaH
$-! % allows some appalling constructs. Is this construct legal, and if so what does this code do?

int a 9 37 b 9 D7 #< # 9 a;;;b<

This question is intended to $e a lighthearted end to the quiz, as, $elie"e it or not, this is perfectly legal syntax# The question is how does the compiler treat it; Those poor compiler writers actually de$ated this issue, and came up with the 3maximum munch3 rule, which stipulates that the compiler should $ite off as $ig (and legal a chunk as it can# :ence, this code is treated as:

# 9 a;; ; b<

Thus, after this code is executed, a B &, $ B L, and c B %G# +f you knew the answer, or guessed correctly, well done# +f you didn/t know the answer then + wouldn/t consider this to $e a pro$lem# + find the greatest $enefit of this question is that it is good for stimulating questions on coding styles, the "alue of code re"iews, and the $enefits of using lint#

+anEay 6huEa

11

You might also like