Monday, April 25, 2011

Embedded C Programmers Interview Questions Part 1

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 answer.

#define SECONDS_PER_YEAR
(60 * 60 * 24 * 365)UL

2. Write the "standard" MIN macro-that is, a macro that takes two arguments and returns the smaller of the two arguments.

#define MIN(A,B)
((A)
<
= (B) ? (A) : (B))

3. Infinite loops often arise in embedded systems. How does you code an infinite loop in C?
There are several solutions to this question.
while(1)
{
}
Many programmers seem to prefer:
for(;;)
{
}
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.

4. 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 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

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 a[10]; // An array of 10 integers
e) int *a[10]; // 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 (*a[10])(int); // An array of 10 pointers to functions that take an integer argument and return an integer

5. What are the uses of the keyword static?
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 body of a function) is accessible by all functions within that module. It is not accessible by functions within any other module.
  • Functions declared static within a module may only be called by other functions within that module. That is, the scope of the function is localized to the module within which it is declared.
6. 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 assumptions 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 referenced within an interrupt service routine
  • Variables shared by multiple tasks in a multi-threaded application



No comments:

Post a Comment