Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 10 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
基本区别在于函数是编译的而宏是预处理的。当您使用函数调用时,它将被转换为 ASM CALL,其中包含所有这些堆栈操作来传递参数和返回值。当您使用宏时,C 预处理器将使用宏转换所有字符串,然后进行编译。
使用宏的缺点是它们隐藏了实现。如果有 bug,那就更难找到了。
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.
在 C(和 C++)中,宏是预处理器指令。这意味着在您的程序开始编译之前,它将遍历并处理您的所有宏。宏很有用,因为
缺点
示例
函数是一段可以相对独立执行并完成特定任务的代码。您可以将其视为数学函数:给定一组输入的函数将给出特定的输出。在 C 中,这些被定义为
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
您必须将宏视为文本替换:就像每次在代码中看到宏时都内联宏代码一样。这对于“代码片段”很有用,因为您可以避免函数调用开销,因为每次调用函数时,您都需要花费一些精力将参数推入堆栈。
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.
另一个区别是,在函数中存在堆栈开销,但在宏的情况下则没有堆栈开销;这只是代码的扩展。
And another difference is that in function there is stack overhead but in case of macros there is no stack overhead; it is just the expansion of code.
函数是从值到值的操作,即您通常认为程序操作的数据类型(数字、字符串等)。
宏是从代码到代码的操作。它获取程序的一部分并使用它为程序生成不同的部分。
C 中的函数和宏之间完全没有重叠;他们不做同样的事情。 (你不能将一个函数从一个值编写为代码;尽管表面上看你不能将一个宏从代码编写为一个值。我知道看起来你可以,但重要的是要明白这并不是你实际上的样子)
可以使宏看起来像一个函数,因为您可以编写一个宏来处理本身生成或表示一个值的一段代码,但该宏仍然不对值本身进行操作:它采用值生成代码(可能是一个简单的数字)并将其编织成值消耗代码(这看起来像是宏的“主体”)。这意味着使用像函数这样的宏非常令人困惑,而且不是它们最适合的用途。相比之下,函数实际上是单个离散的代码块。
函数通常在运行时运行,而宏(在 C 中)总是在编译时运行,这一事实只是由于值通常是动态的以及代码通常在运行时不可用而施加的限制。它实际上不是函数或宏的基本方面(函数可以内联和优化;宏可以应用于动态生成的代码),并且有点转移注意力。
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.
MACRO的优点是,我们只定义一次,如果我们想改变这个值,我们只能在一个地方进行改变,并且值会在整个程序中得到反映。
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.