Saturday, September 20, 2008

Interview FAQs: C programming

1.  What is the significance of the Keyword "static" in C ?  [Believe me 99% of programmers either give a completely wrong answer or an incomplete one ]

[Answer] Well the answer I would look forward to is :-
a .  static limits the scope of a variable or a function to a file : NO EXTERNAL LINKAGE 
b.  function :  Implication of point (a) is that a function declared static cannot be invoked from anyother file than in which the function is defined.
c.  variable : 
global static: it is same as a global  variable with just the restriction of scope steming rom point (a) i.e. scope is limited to the file

Static variables are initialized only once.
 
local static :  the value persists across function calls and scope is limited to a block
          
                      

2. Inline Vs Macros

3.  What does the qualifier Volatile signify ? Where would you use them. Give examples.

4.  How about "const" ? 

5.  Can a variable be const and volatile at the same time ? Substatiate your argument. [ONLY If the person being interviewed answers question 3 and 4 correctly ]

6.  What is endian-ness ? Write a small piece of code to output the endian-ness of the architecture

7. What are register variables ? Why cannot we take the address of the register variable ?

8. Are pointers and arrays the same? [one of my favourites]
[Answer] : N0: Pointers and arrays are NOT the same although many programmers believe that they are. K&R are explicitly says that " pointers and arrays are equivalent as formal arguments to a function". That is it.

To clinch the argument (pun intended) consider this :-

int *pointer;
int array[10];

pointer++  /*  Allowed */
array++   /*  Not Allowed  */

Modifying the base address of an array is not permitted. No such restrictions apply to a pointer.

But as formal parameters f0r a function definition, following are equivalent:-

char *s;
and char s[ ];

9.  Why is the lifetime of a local variable only till the duration of the local block whereas static variable retain their values across function calls ?

[Answer] local variables (automatic) are placed on stack, the contents of which depend on the function execution. When a function is being executed , its arguments as well as all the local variables are stored on the stack. As soon as the function exits , this place is freed automatically to make way for the arguments and local variables of the next function.Therefore, an automatical variable loses its value as soon as the function exits.

whereas, a static variable is stored onto the data segment. This segment persists throughout the execution of the program and hence variables stored on the data segment retain their values across function calls.

10. function pointers. Invocation and use.
[Answer] : 
a. Function pointers are variables which point to the address of a function.
b. Function pointers are less error prone than normal pointers as memory is never allocated or deallocated with them
c. protype :  

int (*pFunc)(); /* pFunc is a pointer to a func that returns an int and doesnt take a param */

No actual data is being pointed to yet.

Now,let us assume that there is a function with the following prototype:-
int func( ) ;

In this case the following two operations are equivalent :- 

pFunc = func;
pFunc = & func;

Usage :
 a . To replace the switch/if statements. Most commonly used in implementing state machines
 b.  Implementing call backs


 

No comments: