What is the difference between a macro and a function in C? Please tell me one application where I can use macros and functions?
相关问题
- Multiple sockets for clients to connect to
- Keeping track of variable instances
- What is the best way to do a search in a large fil
- How to get the maximum of more than 2 numbers in V
- glDrawElements only draws half a quad
A function is an operation from values to values, i.e. the kind of data you normally think of a program manipulating (numbers, strings etc.).
A macro is an operation from code to code. It takes a part of a program and uses it to generate a different part for the program.
There is no overlap at all between functions and macros in C; they do not do the same thing. (You cannot write a function from a value to code; you cannot, despite appearances, write a macro from code to a value. I know it looks like you can, but it's important to understand that that isn't what you're actually doing.)
A macro can be made to look like a function, because you can write a macro designed to handle a piece of code that itself generates or represents a value, but that macro is still not operating on the value itself: it is taking the value-generating code (which may be a simple number) and weaving it into value-consuming code (which is what looks like the "body" of the macro). That means that using macros like functions is extremely confusing and not what they are best used for. Functions in contrast actually are a single discrete block of code.
The fact that functions generally run at runtime and macros (in C) always run at compile time is simply a limitation imposed by the fact that values are usually dynamic, and code is usually not available at runtime, respectively. It isn't actually a fundamental aspect of either functions or macros (functions can be inlined and optimised out; macros can be applied to dynamically generated code), and is a bit of a red herring.
Advantage of MACRO is, we define that only once, and if we want to change the value we can make change at only one place, and value gets reflected across the program.
In C (and C++) a macro is a preprocessor directive. This means that before your program starts compiling it will go through and process all your macros. Macros are useful because
Disdvatages
Example
A function is a piece of code that can relatively independently be executed and performs a specific task. You can think of it sort of like a mathematical function: a function given a set of inputs will give a particular output. In C these are defined as
Advantages and Disadvantages of Macros
Advantages
Disadvantages
You have to think the macro just as a text replacement: is like you inline the macro code every time you see the macro in your code. This is good for "code snippets" because you avoid the function calling overhead, because every time you call a function you have some effort in pushing parameters onto the stack.
The basic difference is that function is compiled and macro is preprocessed. When you use a function call it will be translated into ASM CALL with all these stack operations to pass parameters and return values. When you use a MACRO, C preprocessor will translate all strings using macro and than compile.
Minus of using macros is that they hide implementation. Its way harder to find bug if have one.