Click on the above “Hundred Questions Technology”, select “Pin to Public Account”
Embedded essentials delivered promptly
Some students suggested that we post more interview questions to help them prepare, so here it is.
Reprinted fromCU Technology Community, original title: Classic Review: 16 Embedded C Language Interview Questions
I have cut quite a bit from the original, and strictly speaking, the original text is not very fluent, with some sentences being unclear. However, I still want to thank the authorBut_Beautiful.
Preprocessor
1. Use the preprocessor directive#define to declare a constant indicating how many seconds are in a year (ignoring leap years)
Answer: #define SECONDS_PER_YEAR (60 * 60 * 24 * 365)UL
Here are a few things I want to see:
1) Basic knowledge of the #define syntax (for example: it should not end with a semicolon, use of parentheses, etc.)2) Understanding that the preprocessor will calculate the value of the constant expression for you, so it’s clearer and cost-free to directly show how you calculate the number of seconds in a year rather than writing out the actual value.3) Realizing that this expression will cause an integer overflow on a 16-bit machine, hence the need for the long integer suffixL to inform the compiler that this constant is a long integer.4) If you use UL (indicating unsigned long) in the expression, it might leave a good first impression on the interviewer. Remember, first impressions are important.
2. Write a “standard“ macroMIN that takes two parameters and returns the smaller one.
Answer: #define MIN(A,B) ( (A) <= (B) ? (A) : (B))
This test is set up for the following purposes:
1) Identifying basic knowledge of applying #define in macros. This is important because before inline operators became part of standard C, macros were the only convenient way to generate inline code, which is often necessary for embedded systems to achieve required performance.2) Understanding the importance of carefully enclosing parameters in parentheses within macros.3) I also use this question to start a discussion about macro side effects, such as: what happens when you write the following code?least = MIN(*p++, b);
Infinite Loops
3. Infinite loops are often used in embedded systems. How would you write an infinite loop in C?Answer: This question has several solutions. My preferred solution is:while(1){}Some programmers prefer the following solution:for(;;){}This implementation confuses me because this syntax does not clearly express what is happening. If a candidate gives this as an answer, I will take this as an opportunity to explore their underlying reasoning. If their basic answer is:“I was taught to do it this way, but never thought about why.“This would leave a bad impression on me.The third solution is to usegoto Loop:…goto Loop;If a candidate gives this solution, it suggests they might be an assembly language programmer (which could be good) or someone transitioning from BASIC/FORTRAN programming.
Data Declarations
4. Using the variablea, provide the definitions for the following:a) An integerb) A pointer to an integerc) A pointer to a pointer that points to an integer
(A pointer to a pointer to an integer)d) An array of 10 integerse) An array of 10 pointers to integers.f) A pointer to an array of 10 integersg) A pointer to a function that takes an integer parameter and returns an integer
(A pointer to a function that takes an integer as an argument and returns an integer)h) An array of 10 pointers to functions that take an integer parameter and return an integer
(An array of ten pointers to functions that take an integer argument and return an integer)Answer::
a) int a; b) int *a; c) int **a; d) int a[10];e) int *a[10]; f) int (*a)[10];g) int (*a)(int);h) int (*a[10])(int);
People often claim that several of these questions are the kind that require looking up in a book to answer, and I agree with that. When I wrote this article, I did check a book to confirm the correctness of the syntax. However, during the interview, I expect to be asked this question (or similar ones).
Because during the interview, I am confident that I know the answer to this question. If a candidate does not know all the answers (or at least most of them), then they have not prepared for this interview, and if that candidate is not prepared for this interview, then what can they prepare for?
Static
5. What is the function of the keywordstatic?Answer:This simple question is rarely answered completely. InC language, the keywordstatic has three distinct functions:1) In a function body, a variable declared as static retains its value throughout the function calls.2) Within a module (but outside of function bodies), a variable declared as static can be accessed by functions within the module but not by other functions outside the module. It is a local global variable.3) Within a module, a function declared as static can only be called by other functions within that module. That is, this function is restricted to the local scope of the module that declares it.Most candidates can correctly answer the first part, some can correctly answer the second part, but very few understand the third part. This is a serious shortcoming for a candidate because they clearly do not understand the benefits and importance of localizing data and code scope.
Const
6.What does the keywordconst mean?Answer:As soon as I hear a candidate say:“const means constant“, I know I am dealing with an amateur. Last yearDan Saks summarized all uses ofconst in his article, so every reader ofESP (Embedded Systems Programming) should be very familiar with whatconst can and cannot do. If you have not read that article, as long as you can sayconst means“read-only“, that is sufficient.
Although this answer is not entirely correct, I can accept it. (If you want to know a more detailed answer, carefully readSaks‘s article.)If the candidate can correctly answer this question, I will ask them an additional question: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 have the same effect,a is a constant integer. The third meansa is a pointer to a constant integer (i.e., the integer cannot be modified, but the pointer can). The fourth meansa is a constant pointer to an integer (i.e., the integer pointed to can be modified, but the pointer cannot). The last meansa is a constant pointer to a constant integer (i.e., neither the integer nor the pointer can be modified).
If the candidate can correctly answer these questions, they will leave a good impression on me. By the way, you might ask, even without the keyword const, it is still easy to write functionally correct programs, then why do I place so much importance on the keywordconst?
I have the following reasons:1) The keywordconst serves to convey very useful information to the reader of your code; in fact, declaring a parameter as constant is meant to inform the user of the intended use of that parameter. If you have spent a lot of time cleaning up the mess left by others, you will appreciate this extra information. (Of course, programmers who understand how to useconst rarely leave behind messy code for others to clean up.)2) By providing the optimizer with additional information, using the keywordconst may produce more compact code.3) Using the keywordconst appropriately can naturally protect parameters that should not be changed from being modified by unintended code. In short, this can reduce the occurrence ofbugs.
Volatile
7.What does the keywordvolatile mean? Please give three different examples.Answer:A variable defined asvolatile indicates that this variable may be changed unexpectedly, so the compiler will not assume the value of this variable.
Specifically, the optimizer must carefully re-read the value of this variable every time it is used, rather than using a backup stored in a register. Here are a few examples ofvolatile variables:1) Hardware registers of parallel devices (e.g., status registers)2) Non-automatic variables accessed within an interrupt service routine(Non-automatic variables)3) Variables shared by multiple tasks in multithreaded applicationsAnyone who cannot answer this question will not be hired. I believe this is the most basic question that distinguishes C programmers from embedded system programmers. Embedded programmers often deal with hardware, interrupts,RTOS, and so on, all of which require the use ofvolatile variables.
Not understandingvolatile can lead to disaster.Assuming the candidate correctly answered this question (well, I doubt that will happen), I will delve a bit deeper to see if they really understandvolatile‘s importance.1) Can a parameter be bothconst andvolatile? Explain why.2) Can a pointer bevolatile? Explain why.3) What is wrong with the following function:int square(volatile int *ptr){ return *ptr * *ptr;}Here are the answers:1) Yes. An example is a read-only status register. It isvolatile because it may change unexpectedly. It isconst because the program should not attempt to modify it.2) Yes. Although this is not very common. An example is when an interrupt service routine modifies a pointer to abuffer.3) This code is a bit twisted. The purpose of this code is to return the square of the value pointed to by*ptr, but because*ptr is avolatile parameter, the compiler will generate code like this:int square(volatile int *ptr){ int a,b; a = *ptr; b = *ptr; return a * b;}Since the value of*ptr can change unexpectedly,a andb may have different values. As a result, this code may return a value that is not the expected square. The correct code is as follows:long square(volatile int *ptr){ int a; a = *ptr; return a * a;}Bit Manipulation
8. Embedded systems always require users to perform bit manipulation on variables or registers. Given an integer variablea, write two pieces of code: the first setsa‘sbit 3, and the second clearsa ‘sbit 3. In both operations, other bits should remain unchanged.
Answer:There are three basic responses to this question:1) I don’t know how to start. This indicates that the candidate has never done any embedded systems work.2) Usingbit fields. Bit fields are something that is thrown into the corner of the C language, ensuring that your code is non-portable across different compilers while also guaranteeing that your code is non-reusable.
I recently unfortunately sawInfineon write drivers for its more complex communication chips usingbit fields, thus rendering it completely useless to me as my compiler implementsbit fields in other ways. Morally speaking: never let a non-embedded person touch actual hardware.3) Using#defines andbit masks. This is a highly portable method and is the method that should be used. The best solution is as follows:#define BIT3 (0x1 << 3)static int a;void set_bit3(void){ a |= BIT3;}void clear_bit3(void){ a &= ~BIT3;}Some people prefer to define a mask for setting and clearing values while also defining some explanatory constants, which is also acceptable. I hope to see several points: explanatory constants,|= and&=~ operations.
Accessing Fixed Memory Locations
9. Embedded systems often require programmers to access specific memory locations. In a certain project, it is required to set the value of an integer variable at the absolute address0x67a9 to0xaa66. The compiler is a pureANSI compiler. Write code to accomplish this task.
Answer:This question tests whether you know that it is legal to typecast an integer to a pointer to access an absolute address. The implementation of this question varies with personal style. Typical code might look like this:int *ptr;ptr = (int *)0x67a9;*ptr = 0xaa55;A more obscure method is: *(int * const)(0x67a9) = 0xaa55;Even if your taste is closer to the second solution, I suggest you use the first one during the interview.Interrupts
10. Interrupts are an important component of embedded systems, leading many compiler vendors to provide an extension that allows standardC to support interrupts. A representative fact is the introduction of a new keyword__interrupt. The following code uses the__interrupt keyword to define an interrupt service routine (ISR); please comment on this code.__interrupt double compute_area (double radius){ double area = PI * radius * radius; printf(“\nArea = %f”, area); return area;}Answer:This function has so many errors that it is hard to know where to start:1) ISR cannot return a value. If you do not understand this, you will not be hired.2) ISR cannot pass parameters. If you do not see this, your chances of being hired are equivalent to the first point.3) In many processors/compilers, floating point operations are generally non-reentrant. Some processors/compilers require saving registers, while some processors/compilers do not allow floating point operations within an ISR. Furthermore,ISRs should be short and efficient; performing floating point operations within anISR is unwise.4) Related to the third point,printf() often has reentrancy and performance issues. If you miss the third and fourth points, I won’t be too hard on you. However, if you can answer the last two points, your hiring prospects will improve significantly.
Code Examples
11. What is the output of the following code, and why?void foo(void){ unsigned int a = 6; int b = -20; (a+b > 6) ? puts(“> 6”) : puts(“<= 6”);}Answer:This question tests whether you understand the rules of integer automatic conversion inC. I find that some developers understand very little about these things; however, the answer to this unsigned integer question is that the output is “>6”. The reason is that when there is both signed and unsigned types in an expression, all operands are automatically converted to unsigned types.
Thus,-20 becomes a very large positive integer, so the expression evaluates to greater than6. This is very important for embedded systems that frequently use unsigned data types. If you get this question wrong, you will not get the job.
Previous: S3C2440 Clock System & Programming to Improve Runtime Clock