Set - 4

Question 6 :

What is the benefit of using const for declaring constants ?

Answer :

The benefit of using the const keyword is that the compiler might be able to make optimizations based on the knowledge that the value of the variable will not change. In addition, the compiler will try to ensure that the values won't be changed inadvertently.

Of course, the same benefits apply to #defined constants. The reason to use const rather than #define to define a constant is that a const variable can be of any type (such as a struct, which can't be represented by a #defined constant).

Also, because a const variable is a real variable, it has an address that can be used, if needed, and it resides in only one place in memory


Question 7 :

What is the easiest sorting method to use ?

Answer :

The answer is the standard library function qsort(). It's the easiest sort by far for several reasons:

It is already written.
It is already debugged.
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));

 


Question 8 :

Is it better to use a macro or a function ?

Answer :

The answer depends on 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 inserted directly into your source code at the point where the macro is called.

There is no overhead involved in using a macro like there is in placing a call to a function. However, macros are generally small and cannot handle large, complex coding constructs.

A function is more suited for this type of situation. Additionally, macros are expanded inline, which means that the code is replicated for each occurrence of a macro. Your code therefore could be somewhat larger when you use macros than if you were to use functions.

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 use functions for larger coding tasks that might require several lines of code.


Question 9 :

What are the standard predefined macros ?

Answer :

The ANSI C standard defines six predefined macros for use in the C language: 

Macro Name Purpose
_ _LINE_ _ Inserts the current source code line number in your code. 
_ _FILE_ _ Inserts the current source code filename in your code. 
_ _ Inserts the current date of compilation in your code. 
_ _TIME_ _ Inserts the current time of compilation in your code. 
_ _STDC_ _ Is set to 1 if you are enforcing strict ANSI C conformity. 
_ _cplusplus Is defined if you are compiling a C++ program.