版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、A C Test: The 0 x10 Best Questions for Would-be Embedded Programmers C語(yǔ)言測(cè)試:嵌入式程序員10個(gè)最佳面試問(wèn)題Nigel Jones Pencils up, everyone. Heres a test to identify potential embedded programmers or embedded programmers with potential A n obligatory and significant part of the recruitment process for embedded syste
2、ms programmers seems 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 these tests can be informative for both the interviewer and interviewee. Furthermore, when given outside the pressure of an interview situation, these tests can
3、also be quite entertaining. From the interviewees perspective, you can learn a lot about the person who has written or administered the test. Is the test designed to show off the writers knowledge of the minutiae of the ANSI standard rather than to test practical know-how? Does it test ludicrous kno
4、wledge, 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 these are true, then I know I
5、would seriously doubt whether I want the job in question. From the interviewers perspective, a test can reveal several things about the candidate. Primarily, you can determine the level of the candidates knowledge of C. However, its also interesting to see how the person responds to questions to whi
6、ch they dont 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 see it as an opportunity to learn something? I find this information as useful as
7、their raw performance on the test. With these 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 seeking a job writing compilers! The questions are almost all drawn from situations I have e
8、ncountered 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 seasoned veterans should do very well. Points are not assigned to each question, as this te
9、nds to arbitrarily weight certain questions. However, if you choose to adapt this test for your own uses, feel free to assign scores. Preprocessor 1. 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 answ
10、er. #define SECONDS_PER_YEAR (60 * 60 * 24 * 365)ULIm looking for several things here: Basic knowledge of the #define syntax (for example, no semi-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 cl
11、earer, and penalty-free, to spell out how you are calculating the number of seconds in a year, rather than actually doing the calculation yourself 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 varia
12、ble 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
13、(A,B)(A) = (B) ? (A) : (B)The purpose of this question is to test the following: Basic knowledge of the #define directive as used in macros. This is important because until the inline operator becomes part of standard C, macros are the only portable way of generating inline code. Inline code is ofte
14、n necessary in embedded systems in order to achieve the required performance level Knowledge of the ternary conditional operator. This operator exists in C because it allows the compiler to produce more optimal code than an if-then-else sequence. Given that performance is normally an issue in embedd
15、ed systems, knowledge and use of this construct is important Understanding of the need to very carefully parenthesize arguments to macros I 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 = MIN(*p+, b);3. What i
16、s the purpose of the preprocessor directive #error? Either you know the answer to this, or you dont. If you dont, see Reference 1. This question is useful for differentiating between normal folks and the nerds. Only the nerds actually read the appendices of C textbooks to find out about such things.
17、 Of course, if you arent looking for a nerd, the candidate better hope she doesnt know the answer. Infinite loops 4. Infinite loops often arise in embedded systems. How does you code an infinite loop in C? There are several solutions to this question. My preferred solution is: while(1)Many programme
18、rs seem to prefer: for(;)This construct puzzles me because the syntax doesnt exactly spell out whats going on. Thus, if a candidate gives this as a solution, Ill use 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
19、t thought about it since, it tells me something (bad) about them. A third solution is to use a goto : Loop:.goto Loop;Candidates who propose this are either assembly language programmers (which is probably good), or else they are closet BASIC/FORTRAN programmers looking to get into a new field. Data
20、 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 10 integers g) A pointer to a function that takes
21、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 a10; / An array of 10 in
22、tegers e) int *a10; / 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 (*a10)(int); / An array of 10 pointers to functions that take an integer argument
23、 and return an integer People often claim that a couple of these 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 close to it) when Im being
24、 interviewed. Consequently, I make sure I know the answers, at least for the few hours of the interview. Candidates who dont know all the answers (or at least most of them) are simply unprepared for the interview. If they cant be prepared for the interview, what will they be prepared for? Static 6.
25、What are the uses of the keyword static? This simple question is rarely answered completely. Static has three distinct uses 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 bod
26、y 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 l
27、ocalized to the module within which it is declared Most candidates get the first part correct. A reasonable number get the second part correct, while a pitiful number understand the third answer. This is a serious weakness in a candidate, since he obviously doesnt understand the importance and benef
28、its 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 Im dealing with an amateur. Dan Saks has exhaustively covered const in the last year, such that every reader of ESP should be extremely familiar
29、with what const can and cannot do for you. If you havent been reading that column, suffice it to say that const means read-only. Although this answer doesnt really do the subject justice, Id accept it as a correct answer. (If you want the detailed answer, read Saks columns-carefully!) If the candida
30、te gets the answer correct, Ill ask him these supplemental questions: What do the following declarations mean? const int a;int const a;const int *a;int * const 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
31、integer (that is, the integer isnt 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
32、integer pointed to by a, nor the pointer itself may be modified). If the candidate correctly answers these questions, Ill be impressed. Incidentally, you might wonder why I put so much emphasis on const, since it is easy to write a correctly functioning program without ever using it. I have several
33、reasons: The use of const conveys some very useful information to someone reading your code. In effect, declaring a parameter const tells the user about its intended usage. If you spend a lot of time cleaning up the mess left by other people, youll quickly learn to appreciate this extra piece of inf
34、ormation. (Of course, programmers who use 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 uses const liberally is inherently protected by the compiler against inadvertent coding con
35、structs 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 use. A volatile variable is one that can change unexpectedly. Consequently, the compiler can make no assump
36、tions about the value of the variable. In particular, the optimizer must be careful to reload the variable every time it is used instead of holding a copy in a register. Examples of volatile variables are: Hardware registers in peripherals (for example, status registers) Non-automatic variables refe
37、renced within an interrupt service routine Variables shared by multiple tasks in a multi-threaded application Candidates who dont know the answer to this question arent hired. I consider this the most fundamental question that distinguishes between a C programmer and an embedded systems programmer.
38、Embedded folks deal with hardware, interrupts, RTOSes, and the like. All of these 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 see if th
39、ey really understand the full significance of volatile . In particular, Ill ask them the following additional questions: Can a parameter be both const and volatile ? Explain. Can a pointer be volatile ? Explain. Whats wrong with the following function?: int square(volatile int *ptr)return *ptr * *pt
40、r;The answers are as follows: Yes. An example is a read-only status register. It is volatile because it can change unexpectedly. It is const because the program should not attempt to modify it Yes, although this is not very common. An example is when an interrupt service routine modifies a pointer t
41、o 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;Bec
42、ause its possible for the value of *ptr to change unexpectedly, it is possible for a and b to be different. Consequently, 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 sys
43、tems always require the user to manipulate bits in registers or variables. Given an integer variable a, write two code fragments. The first should set bit 3 of a. The second should clear bit 3 of a. In both cases, the remaining bits should be unmodified. These are the three basic responses to this q
44、uestion: No idea. The interviewee cannot have done any embedded systems work Use 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
45、 the misfortune to look at a driver written by Infineon for one of their more complex communications chips. It used bit fields and was completely useless because my compiler implemented the bit fields the other way around. The moral: never let a non-embedded person anywhere near a real piece of hard
46、ware! Use #defines and bit masks. This is a highly portable method and is the one that should be used. My optimal solution to this problem would be: #define BIT3(0 x1 6) ? puts( 6) : puts( 6. The reason for this is that expressions involving signed and unsigned types have all operands promoted to un
47、signed types. Thus 20 becomes a very large positive integer and the expression evaluates to greater than 6. This is a very important point in embedded systems where unsigned data types should be used frequently (see Reference 2). If you get this one wrong, you are perilously close to not getting the
48、 job. 13. Comment on the following code fragment. unsigned int zero = 0;unsigned int compzero = 0 xFFFF;/*1s 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 understa
49、nds the importance of word length on a computer. In my experience, 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 demora
50、lized-or theyre on a roll and having a good time. If its obvious that the candidate isnt very good, then the test is terminated at this point. However, if the candidate is doing well, then I throw in these supplemental questions. These questions are hard, and I expect that only the very best candida
51、tes will do well on them. In posing these questions, Im looking more at the way the candidate tackles the problems, rather than the answers. Anyway, have fun. Dynamic memory allocation 14. Although not as common as in non-embedded computers, embedded systems do still dynamically allocate memory from
52、 the heap. What are the problems with dynamic memory allocation in embedded systems? Here, I expect the user to mention memory fragmentation, problems with garbage collection, variable execution time, and so on. This topic has been covered extensively in ESP , mainly by P.J. Plauger. His explanation
53、s are far more insightful than anything I could offer here, so go and read those back issues! Having lulled the candidate into a sense of false security, I then offer up this tidbit: What does the following code fragment output and why? char *ptr;if (ptr = (char *)malloc(0) = NULL) elseputs(Got a nu
54、ll 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 t
55、he interviewee thinks this is the correct thing for the library routine to do. Getting the right answer here is not nearly as important as the way you approach the problem and the rationale for your decision. Typedef 15. Typedef is frequently used in C to declare synonyms for pre-existing data types
56、. 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 springs to mind). The answer is the typedef is preferred. Consider
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 沈陽(yáng)高中語(yǔ)文試題及答案
- 融媒體招聘考試試題及答案
- 輔警入警培訓(xùn)課件模板
- 輔助生殖技術(shù)176號(hào)文件
- 《GAT 1400.2-2017公安視頻圖像信息應(yīng)用系統(tǒng) 第2部分:應(yīng)用平臺(tái)技術(shù)要求》專題研究報(bào)告
- 2026 年初中英語(yǔ)《形容詞》專項(xiàng)練習(xí)與答案 (100 題)
- 《GAT 167-2019法醫(yī)學(xué) 中毒尸體檢驗(yàn)規(guī)范》專題研究報(bào)告
- 2026年深圳中考英語(yǔ)拔尖培優(yōu)特訓(xùn)試卷(附答案可下載)
- 2026年大學(xué)大二(交通運(yùn)輸)交通規(guī)劃理論階段測(cè)試試題及答案
- 2026年深圳中考數(shù)學(xué)沖刺實(shí)驗(yàn)班專項(xiàng)試卷(附答案可下載)
- 江蘇省無(wú)錫市2024-2025學(xué)年高一上學(xué)期期末化學(xué)試題
- 2025年統(tǒng)編版五年級(jí)上冊(cè)語(yǔ)文期末專項(xiàng)訓(xùn)練:字音、字形、字義(含答案)
- 睡眠對(duì)考試的重要性
- 網(wǎng)絡(luò)輿情態(tài)勢(shì)感知系統(tǒng)-洞察分析
- 高思導(dǎo)引3-6年級(jí)分類題目-數(shù)字謎02-三下02-簡(jiǎn)單乘除法豎式
- 情侶自愿轉(zhuǎn)賬贈(zèng)與協(xié)議書范本
- 2024-2030年中國(guó)異辛烷行業(yè)市場(chǎng)發(fā)展趨勢(shì)與前景展望戰(zhàn)略分析報(bào)告
- 力士樂(lè)液壓培訓(xùn)教材
- JJG 692-2010無(wú)創(chuàng)自動(dòng)測(cè)量血壓計(jì)
- 人教版四年級(jí)數(shù)學(xué)下冊(cè)第四單元大單元教學(xué)任務(wù)單
- 旋挖鉆孔灌注樁施工記錄表(新)
評(píng)論
0/150
提交評(píng)論