printf("less");
else
if(i>j)
printf("greater");
else
if(i==j)
printf("equal");
}
Answer:
less
10
Givetheoutputoftheprogram
voidmain()
{
char*s="12345sn";
printf("%d",sizeof(s));
}
Answer:
4
11
Question:
Givetheoutputoftheprogram
voidmain()
{
inti;
for(i=1;i<4,i++)
switch(i)
case1:
printf("%d",i);break;
{
case2:
printf("%d",i);break;
case3:
printf("%d",i);break;
}
switch(i)case4:
printf("%d",i);
}
Answer:
1,2,3,4
12.
Question:
Howtopasstwoargumentstoafunctionpromptedtobyfunctionpointer
A.g->(1,2)
B.*g(1,2)
C.(*g)(1,2)
D.g(1,2)
Answer:
C
13
Question:
Canyouhaveconstantvolatilevariable?
Answer:
YES.Wecanhaveaconstvolatilevariable.
avolatilevariableisavariablewhichcanbechangedbytheextrenalevents(likeaninterrputtimerswillincrementthevoltilevarible.Ifyoudontwantyouvolatilevaribaletobechangedthendeclarethemas“constvolatile”.
14.
Question:
studythecode:
#include
voidmain()
{
constinta=100;
int*p;
p=&a;
(*p)++;
printf("a=%d\n(*p)=%d\n",a,*p);
}
Whatisprinted?
A)100,101B)100,100C)101,101D)Noneoftheabove
AnswerC
EmbeddedC
1.Usingthe#definestatement,howwouldyoudeclareamanifestconstantthatreturnsthenumberofsecondsinayear?
Disregardleapyearsinyouranswer.
Answer
#defineSECONDS_PER_YEAR(60*60*24*365)UL
I'mlookingforseveralthingshere:
Basicknowledgeofthe#definesyntax(forexample,nosemi-colonattheend,theneedtoparenthesize,andsoon)
Anunderstandingthatthepre-processorwillevaluateconstantexpressionsforyou.Thus,itisclearer,andpenalty-free,tospellouthowyouarecalculatingthenumberofsecondsinayear,ratherthanactuallydoingthecalculationyourself
Arealizationthattheexpressionwilloverflowanintegerargumentona16-bitmachine-hencetheneedfortheL,tellingthecompilertotreatthevariableasaLong
Asabonus,ifyoumodifiedtheexpressionwithaUL(indicatingunsignedlong),thenyouareofftoagreatstart.Andremember,firstimpressionscount!
2.Writethe"standard"MINmacro-thatis,amacrothattakestwoargumentsandreturnsthesmallerofthetwoarguments.
Answer:
#defineMIN(A,B)((A)<=(B)?
(A):
(B))
Thepurposeofthisquestionistotestthefollowing:
Basicknowledgeofthe#definedirectiveasusedinmacros.ThisisimportantbecauseuntiltheinlineoperatorbecomespartofstandardC,macrosaretheonlyportablewayofgeneratinginlinecode.Inlinecodeisoftennecessaryinembeddedsystemsinordertoachievetherequiredperformancelevel
Knowledgeoftheternaryconditionaloperator.ThisoperatorexistsinCbecauseitallowsthecompilertoproducemoreoptimalcodethananif-then-elsesequence.Giventhatperformanceisnormallyanissueinembeddedsystems,knowledgeanduseofthisconstructisimportant
Understandingoftheneedtoverycarefullyparenthesizeargumentstomacros
Ialsousethisquestiontostartadiscussiononthesideeffectsofmacros,forexample,whathappenswhenyouwritecodesuchas:
least=MIN(*p++,b);
3.Infiniteloopsoftenariseinembeddedsystems.HowdoesyoucodeaninfiniteloopinC?
Thereareseveralsolutionstothisquestion.Mypreferredsolutionis:
Answer:
while
(1)
{
?
}
5.Usingthevariablea,givedefinitionsforthefollowing:
a)Aninteger
b)Apointertoaninteger
c)Apointertoapointertoaninteger
d)Anarrayof10integers
e)Anarrayof10pointerstointegers
f)Apointertoanarrayof10integers
g)Apointertoafunctionthattakesanintegerasanargumentandreturnsaninteger
h)Anarrayoftenpointerstofunctionsthattakeanintegerargumentandreturnaninteger
Answers:
a)inta;//Aninteger
b)int*a;//Apointertoaninteger
c)int**a;//Apointertoapointertoaninteger
d)inta[10];//Anarrayof10integers
e)int*a[10];//Anarrayof10pointerstointegers
f)int(*a)[10];//Apointertoanarrayof10integers
g)int(*a)(int);//Apointertoafunctionathattakesanintegerargumentandreturnsaninteger
h)int(*a[10])(int);//Anarrayof10pointerstofunctionsthattakeanintegerargumentandreturnaninteger
Peopleoftenclaimthatacoupleofthesearethesortsofthingthatonelooksupintextbooks-andIagree.Whilewritingthisarticle,Iconsultedtextbookstoensurethesyntaxwascorrect.However,Iexpecttobeaskedthisquestion(orsomethingclosetoit)whenI'mbeinginterviewed.Consequently,ImakesureIknowtheanswers,atleastforthefewhoursoftheinterview.Candidateswhodon'tknowalltheanswers(oratleastmostofthem)aresimplyunpreparedfortheinterview.Iftheycan'tbepreparedfortheinterview,whatwilltheybepreparedfor?
6.Whataretheusesofthekeywordstatic?
Answer:
StatichasthreedistinctusesinC:
Avariabledeclaredstaticwithinthebodyofafunctionmaintainsitsvaluebetweenfunctioninvocations
Avariabledeclaredstaticwithinamodule,(butoutsidethebodyofafunction)isaccessiblebyallfunctionswithinthatmodule.Itisnotaccessiblebyfunctionswithinanyothermodule.Thatis,itisalocalizedglobal
Functionsdeclaredstaticwithinamodulemayonlybecalledbyotherfunctionswithinthatmodule.Thatis,thescopeofthefunctionislocalizedtothemodulewithinwhichitisdeclared
Mostcandidatesgetthefirstpartcorrect.Areasonablenumbergetthesecondpartcorrect,whileapitifulnumberunderstandthethirdanswer.Thisisaseriousweaknessinacandidate,sinceheobviouslydoesn'tunderstandtheimportanceandbenefitsoflocalizingthescopeofbothdataandcode.
7.Whatdothefollowingdeclarationsmean?
constinta;
intconsta;
constint*a;
int*consta;
intconst*consta;
Answer
Thefirsttwomeanthesamething,namelyaisaconst(read-only)integer.Thethirdmeansaisapointertoaconstinteger(thatis,theintegerisn'tmodifiable,butthepointeris).Thefourthdeclaresatobeaconstpointertoaninteger(thatis,theintegerpointedtobyaismodifiable,butthepointerisnot).Thefinaldeclarationdeclaresatobeaconstpointertoaconstinteger(thatis,neithertheintegerpointedtobya,northepointeritselfmaybemodified).
8.Whatdoesthekeywordvolatilemean?
Givethreedifferentexamplesofitsuse.
Answer:
Avolatilevariableisonethatcanchangeunexpectedly.Consequently,thecompilercanmakenoassumptionsaboutthevalueofthevariable.Inparticular,theoptimizermustbecarefultoreloadthevariableeverytimeitisusedinsteadofholdingacopyinaregister.Examplesofvolatilevariablesare:
Hardwareregistersinperipherals(forexample,statusregisters)
Non-automaticvariablesreferencedwithinaninterruptserviceroutine
Variablessharedbymultipletasksinamulti-threadedapplication
9.
Canapointerb