C语言 C中的宏和函数有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4990362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between a macro and a function in C?
提问by user615929
What is the difference between a macro and a function in C? Please tell me one application where I can use macros and functions?
C 中的宏和函数有什么区别?请告诉我一个可以使用宏和函数的应用程序?
回答by Alexander Sobolev
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.
基本的区别是函数是编译的,宏是预处理的。当您使用函数调用时,它将被转换为 ASM CALL,并带有所有这些堆栈操作以传递参数和返回值。当您使用宏时,C 预处理器将使用宏而不是编译来翻译所有字符串。
Minus of using macros is that they hide implementation. Its way harder to find bug if have one.
使用宏的缺点是它们隐藏了实现。如果有一个,它更难找到错误。
回答by Jim Jeffries
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
在 C(和 C++)中,宏是一个预处理器指令。这意味着在您的程序开始编译之前,它将通过并处理您的所有宏。宏很有用,因为
- They can make your program easier to read
- They can improve efficiency (as they can be calculated at compile time)
- They can shorten long or complicated expressions that are used a lot. For example, we use a macro to get the current log4cpp logger and another few to write to it with various levels.
- 它们可以使您的程序更易于阅读
- 它们可以提高效率(因为它们可以在编译时计算)
- 它们可以缩短经常使用的冗长或复杂的表达式。例如,我们使用一个宏来获取当前的 log4cpp 记录器,并使用其他几个以不同级别写入它。
Disdvatages
缺点
- Expand the size of your executable
- Can flood your name space if not careful. For example, if you have too many preprocessor macros you can accidentally use their names in your code, which can be very confusing to debug.
- 扩展可执行文件的大小
- 如果不小心,可能会淹没您的名称空间。例如,如果您有太多的预处理器宏,您可能会不小心在代码中使用它们的名称,这会导致调试非常混乱。
Example
例子
#define INCREMENT(x) x++
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
函数是可以相对独立地执行并执行特定任务的一段代码。你可以把它想象成一个数学函数:给定一组输入的函数将给出一个特定的输出。在 C 中,这些定义为
<return type> <name>(<parameters>)
{
//code body
}
回答by Felice Pollano
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.
您必须将宏视为文本替换:就像您每次在代码中看到宏时都内联宏代码一样。这对“代码片段”很有好处,因为您避免了函数调用开销,因为每次调用函数时,您都需要努力将参数推入堆栈。
回答by Ariful Hoque
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.
另一个区别是在函数中有堆栈开销,但在宏的情况下没有堆栈开销;这只是代码的扩展。
回答by Leushenko
A function is an operation from valuesto values, i.e. the kind of data you normally think of a program manipulating (numbers, strings etc.).
函数是从值到值的操作,即您通常认为程序操作的数据类型(数字、字符串等)。
A macro is an operation from codeto code. It takes a part of a program and uses it to generate a different part for the program.
宏是从代码到代码的操作。它获取程序的一部分并使用它为程序生成不同的部分。
There is no overlap at allbetween 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.)
有在所有没有重叠在C函数和宏之间; 他们不做同样的事情。(你不能从一个值到代码写一个函数;你不能,尽管看起来,从代码到一个值写一个宏。我知道看起来你可以,但重要的是要明白这不是你实际上的样子正在做。)
A macro can be made to looklike 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 notoperating 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 area 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.
函数通常在运行时运行而宏(在 C 中)总是在编译时运行的事实只是由于值通常是动态的,并且代码通常在运行时通常不可用而强加的限制。它实际上不是函数或宏的基本方面(函数可以被内联和优化出来;宏可以应用于动态生成的代码),并且有点牵强附会。
回答by Ravi
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.
MACRO 的优点是,我们只定义一次,如果我们想改变值,我们可以只在一个地方改变,并且值在整个程序中得到体现。

