C Interview Questions Part 22

Q:Why n++ executes quicker compared to n+1 ?
A:The expression n++ needs a single machine instruction such as INR to carry out the increment operation whereas, n+1 needs more instructions to carry out this operation.

Q:What is the purpose of main( ) function ?
A:The function main( ) invokes other functions within it.It is the first function to be called when the program starts execution.It returns an int value to the environment that called the program.Recursive call is allowed for main( ) also.It has two arguments 1)argument count and 2) argument vector (represents strings passed).

Q:What is the simplest sorting method to use?
A:The answer is the standard library function qsort(). It is already written and already debugged and also it has been optimized as much as possible (usually).

Void qsort(void *buf, size_t num, size_t size, int (*comp)(const void *ele1, const void *ele2));

Q:What will the preprocessor do for a program?
A:The C preprocessor is used to modify your program according to the preprocessor directives in your source code. A preprocessor directive is a statement (such as #define) that provides the preprocessor specific instructions on how to modify your source code.
 
The preprocessor is actually invoked as the 1st part of your compiler program’s compilation step. It is usually hidden from the programmer since it is run automatically by the compiler.The preprocessor reads in all of your include files and the source code you are compiling and creates a preprocessed version of your source code. This preprocessed version has all of its macros and constant symbols replaced by their corresponding code and value assignments.If your source code consists of any conditional preprocessor directives (such as #if), the preprocessor evaluates the condition and modifies your source code accordingly.

Q:Is it better to use a macro or a function ?
A:The answer depends upon the situation you are writing code for. Macros have the distinct advantage of being more efficient (and faster) than functions, because their corresponding code is actually inserted directly into your source code at the point where the macro is called.However, macros are often small and cannot handle large, complex coding constructs.Thus, the choice between using a macro and using a function is one of deciding between the tradeoff of faster program speed versus smaller program size. Generally, you should use macros to replace small, repeatable code sections, and you should utilize functions for larger coding tasks that may need several lines of code.