|
INTERVIEW QUESTIONS
C
FUNCTIONS IN C
DETAILS
Question: 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.
|
|
|
Category |
Functions in C Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.3) By 7231 users |
Added on |
9/18/2014 |
Views |
69048 |
Rate it! |
|
|
Question:
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. Source: CoolInterview.com
It generally depends on your requirement. Because macro are inserted into the code where they called and the cursor always go sequentially. It means, when a compiler compile the program, it insert the macro code to particular place. And it increases the speed of execution.
But in case of function, whenever it is called the pointer goes to particular address where function is stored and do calculation and return result if required. In this case, if a function is called so many time, it consume time in to and from. Source: CoolInterview.com
Answered by: Jitender Kumar | Date: 12/6/2009
| Contact Jitender Kumar
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
What is the purpose of main( ) function ?
|
View Answer
|
|
How do you use a pointer to a function ?
|
View Answer
|
|
Is it possible to execute code even after the program exits the main() function?
|
View Answer
|
|
Does there exist any other function which can be used to convert an integer or a float to a string ?
|
View Answer
|
|
Advantages of a macro over a function ?
|
View Answer
|
|
What is the difference between printf() and sprintf() ?
|
View Answer
|
|
What is the code for clrscr() function?
|
View Answer
|
|
How to use the cprintf,cscanf,sscanf,sprintf,vsscanf,vsprintf,vscanf & vprintf?
|
View Answer
|
|
How to convert decimal to octal and hexadecimal in c program?
|
View Answer
|
|
How to use floodfill() function and what is the use of kbhit?
|
View Answer
|
|
Write a program with out using main() function?
|
View Answer
|
|
Can there be at lest some solution to determine the number of arguments passed to a variable argument list function?
|
View Answer
|
|
What do the functions atoi(),itoa()and gctv()do?
|
View Answer
|
|
How would you use bsearch()function to search a name stored in array of pointers to string?
|
View Answer
|
|
How would you use the functions sin(),pow(),sqrt()?
|
View Answer
|
|
how would you use the functions memcpy(),memset(),memmove()?
|
View Answer
|
|
How would you use the functions randomize()and random()?
|
View Answer
|
|
How would you use the functions randomize()and random()?
|
View Answer
|
|
what is the difference between the functions rand(),random(),srand() and randomize()?
|
View Answer
|
|
Can you use the function fprintf()to display the output on the screen?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All Functions in C Interview Questions & Answers - Exam Mode /
Learning Mode
|