How to prepare for Embedded C interview

I have compiled some of the common interview questions that you may encounter if you are applying for embedded software position. Hope it benefits you.

Image by Ilya Pavlor at Unsplash

What is the difference between hard real-time and soft real-time OS?

A Hard real-time system strictly adheres to the deadline associated with the task. If the system fails to meet the deadline, even once, the system is considered to have failed. In case of a soft real-time system, missing a deadline is acceptable. In this type of system, a critical real-time task gets priority over other tasks and retains that priority until it completes.

What is volatile ?

Variable that can change unexpectedly

  • hardware registers in peripheral
  • non automatic variable referenced within interrupt service
  • variable shared by multiple task in multiple threaded applications

What is const?

  • Means read only.
const int a; // const integer
Int const a; // const integer
Const int *a; // pointer to const integer. Integer x modify but pointer can
Int * const a; // const pointer integer. Integer can modify, pointer x
Int const * a const; // const pointer to const integer

Can parameter be both const and volatile ?

Yes. Example is read only status register. Volatile cause can change unexpectedly. Const cause program should not change it.

How to declare data

int a ; // an integer
Int *a; // pointer to integer
Int **a; // pointer to pointer to integer
Int a[10]; // array of 10 integers
Int *a[10]; // an array of pointers to integer
Int (*a)[10]; // a pointer to array of 10 integers
Int (a*)(int); // a pointer of function that takes integer argument and returns integer

Explain Union

Useful when need direct access to hardware/memory needed. Share same memory. As long as longest memory.

typedef union{
struct {
unsigned char byte1;
unsigned char byte2;
unsigned char byte3;
unsigned char byte4;
} bytes;
unsigned int dword;
} HW_Register;
HW_Register reg;
reg.dword = 0x12345678;
reg.bytes.byte1 = 1;

Memory segment divided into text, data, heap and stack.

  • Text is the program code.
  • Data is divided into initialised and uninitialised.
  • Initialised means static and global variables initialised with non zero values
  • Uninitialised means static and global variables initialised with zero or not initialised. Also know as BSS — Block Started by Symbol
  • Both Heap and Stack refers to RAM.
  • Heap used for dynamic memory allocation. No enforced pattern to allocation and deallocation. Access is slower.
  • Stack used for static memory allocation. It is reserved in Last In First Out (LIFO). When a function is called, a block is reserved at the top to keep data, when function is returned, the block is then freed.

Assembler, linker and loader

  • Preprocessing is the first pass of any c compilation. It processes include files, conditional compilation instructions and macros.
  • Compilation is the second pass. It takes the output of preprocessor and the source code and generated the assembler source code
  • Assembly is the third stage. It takes the assembly source code and provides an assembly listing with offsets. The output is stored in object file.
  • Listing is the final stage. It takes one or more object files or libraries as input and combines them to produce a single files. In doing so, it resolves references to external symbols, assigns final addresses to procedures / functions and variables and revises code and data to reflect new addresses (a process called relocation).

What is inline function?

  • Inline function is feature to increase execution time of program and reduce function calling overhead.
  • With inline, compiler replaces the function call statement with function code itself. This compile does not have to jump to another location to execute the function.
  • Disadvantage includes increases the executable size due to code expansion.

Explain Macros

Enable assign short names to source code blocks.

#define LEN 128
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define MYMACRO() (func();)

Function of Static?

  • A variable declared static within body of function maintains its value between function invocations.
  • A variable declared static within a module (outside body of function) is accessible by all functions within that module. It is not accessible by functions from other module.
  • Functions declared static within module may only be called by other functions within that module. That is the scope is localised to the module within which it is declared.

How to access fixed memory location

  • Set integer variable at absolute address 0x67a9 to value 0xaa55
Int * ptr;
Ptr = (int *) 0x67a9;
*ptr = 0xaa55;

Standard library header <cstdint>

Int8_t, Int16_t, Int32_t, Int64_t = Signed integer type of width exactly 8, 16, 32, and 64 bits with no padding bit and using 2’s complement for negative values.

Size of variables

  • Char = 1 byte
  • Short / Int = 2 byte
  • Long = 4 byte
  • Pointer (irrespective of type it points to) = 4 bytes (32bit machine), 8 bytes (64bit machine)

NULL and Void Pointer

  • Null pointer is a special reserved value of a pointer. A pointer of any type has such a reserved value. Formally, each specific pointer type (int *, char * etc.) has its own dedicated null-pointer value. Conceptually, when a pointer has that null value it is not pointing anywhere.
  • Void pointer is a specific pointer type — void * — a pointer that points to some data location in storage, which doesn’t have any specific type.

Is Count Down_to_Zero Loop better than Count_Up_Loops?

  • Count down to zero loops are better.
  • Reason behind this is that at loop termination, comparison to zero can be optimized by the compiler.
  • Most processors have instruction for comparing to zero. So they don’t need to load the loop variable and the maximum value, subtract them and then compare to zero. That is why count down to zero loop is better.

Can structures be passed to the functions by value?

  • Passing structure by its value to a function is possible, but not a good programming practice.
  • First of all, if we pass the structure by value and the function changes some of those values, then the value change is not reflected in caller function.
  • Also, if the structure is big, then passing the structure by value means copying the whole structure to the function argument stack which can slow the program by a significant amount.

Reference:

http://a4academics.com/interview-questions/57-c-plus-plus/722-embedded-c-interview-questions?showall=&limitstart=

About the author

Founder of tattweicheah.com. Loves music, sport and most importantly software development.

Leave a Reply

Your email address will not be published. Required fields are marked *