Friday, May 8, 2009

C Interview Questions

C Interview Questions

Types of Functions/Sub-routines

Functions/Sub-routines

  • A function or subroutine or sub-program is a portion of code with in a larger program which performs a specific task and is independent of the remaining code.
  • A function is often coded so that it can be executed (called) several times and/or from several places during a single execution of the program.
  • Once the execution of the function got completed it branch back to the next instruction from where it is called.
  • Advantages of functions:
    • By using functions we can reduce the duplication of code in the program.
    • Enables the reuse of code from multiple programs.
    • Improves the Readability, Maintainability, Extensibility of the program.
    • Dividing the large programming task among various programmers.
    • Hiding Implementation details from the users.
    • Decomposing the complex programming task into simpler tasks.
  • Each function call creates a new entry called a stack frame, at the top of the stack; when function returns, its stack frame gets deleted from the stack. Each stack frame contains the private data of the corresponding call, which typically includes the procedure's parameters, internal variables and return address.
  • Advantage of storing private data on stack is that it allows recursive function calls, since each nested call will get separate instance of the stack frame on stack for private data.
  • Disadvantage of using this stack mechanism is that it includes incrementing and decrementing of stack pointer, and accessing local variables and parameters by frame-relative addresses, instead of absolute addresses.

Callback functions

  • A callback is a executable code that is passed as a argument to a function.
  • Normally callback functions are used to allow the low-level software layer to call a function defined in high-level software layer.
  • Form of callback varies among programming languages
    • C and C++ allow function pointers as arguments to other functions.
    • In Interpreted languages, routine name is passed as a argument to the called routine and routine will be executed using 'eval'
  • Callbacks are also frequently used as a means to handle exceptions arising within the low level function.

Re-entrant functions

  • A Re-entrant function is a function that can be re-entered while it is already running.
  • For a function to be a re-entrant
    • Should not Use any Static or global variables. (But can have Static Constant Data variables).
    • Should not call any Non-Re-entrant functions.
    • Should only use the local variables and the parameters passed.
    • Should not return address to Static or global Non constant data.
    • Should not modify its own code.
  • Recursive functions need to be Re-entrant.
  • Functions which are directly/indirectly called from Interrupt handler typically need to be re-entrant.

Recursive functions

  • A Recursive function is a function which calls by itself.
  • Using recursion, we split a complex problem into its single simplest case.
  • Recursive function should be a re-entrant function.

Interrupt Service Routine


Re-entrant Interrupt Handler

  • Is an Interrupt Handler which re-enables the interrupts early in the interrupt handler.
  • It reduces the interrupt latency.

Inline functions

  • An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. Instead of transferring control to and from the function code segment, a modified copy of the function body may be substituted directly for the function call. In this way, the performance overhead of a function call is avoided. 
  • Using the inline specifier is only a suggestion to the compiler that an inline expansion can be performed; the compiler is free to ignore the suggestion.
  • An Inline function is a programming language construct to tell the compiler it should perform in-line expansion on a particular function.
  • Inline function is used to eliminate the time overhead when a function is called.
  • Static local variables are not allowed to be defined within the body of an inline function.
  • The most efficient way to code an inline function is to place the inline function definition in a header file, and then include the header in any file containing a call to the function which you would like to inline. Otherwise while linking "Unresolved Symbols" error may observe.
  • If both extern and inline keywords are used with function definition then the function will not be inlined.
  • Inline functions can include arguments, local variables, loops, call local functions, call built-in functions, call another inline functions, can return statements but not recursive functions.

Comparisons

Inline functions Vs Macros

  • MACRO invocations don't perform type checking, while inline functions do.
  • Expressions passed as arguments to inline functions are evaluated once. But In some cases, expressions passed as arguments to MACRO's can be evaluated more than once.
  • MACRO can't return something like function would do.
  • MACRO's are textual substitution. So they may result un-intended side effects and inefficiency due to evaluation of arguments and order of operations.
  • Compiler errors with MACRO's are often difficult to understand. 
  • Debugging information for inline code is usually more helpful than that of macro-expanded code.

Compiler Vs Interpreter

Compiler -- spends some time evaluating the entire program and then translates all the programming statements of a program into a machine language program, which is then executed at once. 

A compiler reads the whole source code and translates it into a complete machine code program to perform the required tasks which is output as a new file. This completely separates the source code from the executable file. The biggest advantage of this is that the translation is done once only and as a separate process. The program that is run is already translated into machine code so is much faster in execution. The disadvantage is that you cannot change the program without going back to the original source code, editing that and recompiling.

Interpreter -- translates interactively each programming statement into an immediately usable machine language instruction. Although an interpreter slows down the execution speed of a program somewhat, it does not require extra steps to compile and link like a compiler. 

An interpreter reads the source code one instruction or line at a time, converts this line into machine code and executes it. The machine code is then discarded and the next line is read. The advantage of this is it's simple and you can interrupt it while it is running, change the program and either continue or start again. The disadvantage is that every line has to be translated every time it is executed, even if it is executed many times as the program runs. Because of this interpreters tend to be slow.


Compiler characteristics:

  • spends a lot of time analyzing and processing the program
  • the resulting executable is some form of machine- specific binary code
  • the computer hardware interprets (executes) the resulting code
  • program execution is fast

Interpreter characteristics:

  • relatively little time is spent analyzing and processing the program
  • the resulting code is some sort of intermediate code
  • the resulting code is interpreted by another program
  • program execution is relatively slow