简体   繁体   English

像C中的宏一样的功能

[英]Function like Macros in C

I am trying to understand the idea of function like Macros however there are a few points that befuddle me. 我试图理解像Macros这样的功能的想法,但有几点让我迷惑。 For example say we have: 例如,我们说:

#define Max(a,b)  ((a)>(b)) ? (a):(b))

and I call it like such 我称之为

int i = Max(4,5);

This will evaluate a conditional expression equivalent to a>b ? 这将评估一个等于a>b的条件表达式? If yes then a, else b. 如果是,则a,否则b。 But I'm confused as to how the Max function knows what to do with the arguments. 但我对Max函数如何知道如何处理参数感到困惑。 Unlike an actual function, the implementation isn't written in code in the calling program. 与实际函数不同,实现不是在调用程序的代码中编写的。 is the statement to the right of the define statement doing this for me? 是定义语句右边的声明为我做这个吗? Its just a new thing for me and I want to make sure I understand what is happening here. 这对我来说只是一个新事物,我想确保我理解这里发生的事情。

This particular part of function like macros confuses me. 功能的这个特殊部分就像宏一样迷惑我。 I know that these types of macros are useful for reducing overhead costs since they exclude the JSR RTS processor instructions which saves memory on the stack. 我知道这些类型的宏对于降低开销成本很有用,因为它们排除了在堆栈上节省内存的JSR RTS处理器指令。

#define Max(a,b)  ((a)>(b)) ? (a):(b))

is a macro, that causes nothing else but a simple textual replacement within your code, which means that during the preprocessing this line: 是一个宏,除了代码中的简单文本替换之外别无其他,这意味着在预处理此行期间:

int i = Max(4,5);

is changed into: 变成:

int i = ((4)>(5)) ? (4):(5));

Note that there is no type safety while working with macros like this one and you will have really hard time while debugging your code as well. 请注意,使用像这样的宏时没有类型安全性,并且在调试代码时也会非常困难。 Good rule of thumb is: Don't use macro when you can achieve the same with function : 好的经验法则是: 当你可以实现与功能相同时,不要使用宏

int max(int a, int b) {
    return (a > b) ? a : b;
}

What the compiler actually sees, after preprocessing, is: 在预处理之后,编译器实际看到的是:

int i = ((4)>(5)) ? (4):(5));

The parameters passed to the macro are substituted into the body of the macro. 传递给宏的参数将替换为宏的主体。

Just stop thinking about macro like compilable code. 只是停止考虑像可编译代码的宏。 Macros are "resolved" by pre-processor, not actually during compilation stage. 宏由预处理器“解析”,而不是在编译阶段。 So by macro definitions you just define how to process certain string in text file. 因此,通过宏定义,您只需定义如何处理文本文件中的某些字符串。 Only output of pre-processor is passed to compiler. 只有预处理器的输出传递给编译器。 You can use gcc -E to see your source after pre-processor. 您可以使用gcc -E在预处理器后查看源代码。 It is still C code on this stage but without any preprocessor directive. 它仍然是此阶段的C代码,但没有任何预处理器指令。

Hope this will help you. 希望这会帮助你。

try to build your code with the gcc -E and see how your code look before compiling it 尝试使用gcc -E构建代码,并在编译代码之前查看代码的外观

In fact in the build process the compilator transform your actual code to a preprocessor code. 事实上,在构建过程中,编译器会将您的实际代码转换为预处理器代码。 In the preprocessor phase the compilator replace all macro in your c code with its content and generate another code called preprocessor code and then the compilateor generate the object code from the preprocessor code 在预处理器阶段,编译器将c代码中的所有宏替换为其内容,并生成另一个称为预处理器代码的代码,然后编译器从预处理器代码生成目标代码

The gcc -E allow you to see your preprocessor code gcc -E允许您查看预处理器代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM