C 中的宏和函数有什么区别?

发布于 2024-10-17 10:03:41 字数 1566 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

深爱不及久伴 2024-10-24 10:03:41

基本区别在于函数是编译的而宏是预处理的。当您使用函数调用时,它将被转换为 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.

一刻暧昧 2024-10-24 10:03:41

在 C(和 C++)中,宏是预处理器指令。这意味着在您的程序开始编译之前,它将遍历并处理您的所有宏。宏很有用,因为

  • 它们可以使您的程序更易于阅读
  • 它们可以提高效率(因为它们可以在编译时计算)
  • 它们可以缩短经常使用的长或复杂的表达式。例如,我们使用一个宏来获取当前的 log4cpp 记录器,并使用另外一些宏来以不同级别写入它。

缺点

  • 扩大可执行文件的大小
  • 如果不小心,可能会淹没您的名称空间。例如,如果您有太多预处理器宏,您可能会意外地在代码中使用它们的名称,这可能会导致调试非常混乱。

示例

#define INCREMENT(x) x++

函数是一段可以相对独立执行并完成特定任务的代码。您可以将其视为数学函数:给定一组输入的函数将给出特定的输出。在 C 中,这些被定义为

<return type> <name>(<parameters>)
{
  //code body
}

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

  • 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.

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

<return type> <name>(<parameters>)
{
  //code body
}
旧梦荧光笔 2024-10-24 10:03:41

您必须将宏视为文本替换:就像每次在代码中看到宏时都内联宏代码一样。这对于“代码片段”很有用,因为您可以避免函数调用开销,因为每次调用函数时,您都需要花费一些精力将参数推入堆栈。

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.

不离久伴 2024-10-24 10:03:41

另一个区别是,在函数中存在堆栈开销,但在宏的情况下则没有堆栈开销;这只是代码的扩展。

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.

无敌元气妹 2024-10-24 10:03:41

函数是从的操作,即您通常认为程序操作的数据类型(数字、字符串等)。

宏是从代码代码的操作。它获取程序的一部分并使用它为程序生成不同的部分。

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.

不必你懂 2024-10-24 10:03:41

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文