什么是C宏的有用吗? [英] What are C macros useful for?

查看:116
本文介绍了什么是C宏的有用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一点点的 C ,然后我可以读取它不够好,得到它是做什么的总体思路,但每次我都遇到了一个宏它已经完全甩了我。我最终不得不记住宏是什么,代替它在我的头,我读。我曾经遇到过的是直观,容易理解,总是像小迷你的功能的人,所以我一直想知道为什么他们不只是功能。

我可以理解需要定义不同的生成类型调试或跨平台建立在preprocessor但定义任意替换的能力似乎是有用的,只有使本已困难的语言更是难以理解。

为什么这样一个复杂的preprocessor推出的 C ?并没有任何人有我明白为什么它似乎仍然可以用于简单的比其他目的使用它,这将使的一个例子,如果#调试风格条件编译?

编辑:

看了一些答案,我依然只是不明白这一点。最常见的答案是内联code。如果inline关键字不会做那么无论它有一个很好的理由不这样做,或者实现需要修理。我不明白为什么一个完全不同的机制需要,意思是真正内联这个code(除了形成被写入code之前直列大约为)。我也不明白,提到,如果太傻被放在一个函数的想法。可靠地接受一个输入,并产生一个输出任何一块$ C $的c最好放在一个函数。我想我可能不会得到它,因为我不习惯到微的优化写的 C ,但在preprocessor感觉就像一个复杂的解决方案,以几个简单的问题。


解决方案

  

我最终不得不记住的宏是什么,代替它在我的头,我读。


这似乎对宏的命名不良反映。我会假设你不会有效仿preprocessor如果它是一个 log_function_entry()宏。


  

这是我所遇到的那名直观,易于理解,总是像小迷你函数的人,所以我一直想知道为什么他们刚刚的werent功能。


通常他们是应该的,除非他们需要在通用的参数进行操作。

 的#define MAX(A,B)((a)及LT;(B)(B):( a)条)

将任何类型的工作,一个< 运营商

更多的只是函数,宏让你执行使用源文件中的符号操作。这意味着你可以创建一个新的变量名,或引用源文件和行号的宏上。

在C99,宏还允许您调用可变参数的函数,如printf

 的#define log_message(后卫,格式,...)\\
   如果(后卫)的printf(%S:%D:格式\\ n,__FILE__,__LINE __,__ VA_ARGS_);log_message(富== 7,X%D,X)

在该格式就像printf的。如果后卫是真实的,它与打印的信息文件和行号一起输出消息。如果这是一个函数调用,它不会知道的文件和行你叫它,并使用 vaprintf 将更多的工作。

I have written a little bit of C, and I can read it well enough to get a general idea of what it is doing, but every time I have encountered a macro it has thrown me completely. I end up having to remember what the macro is and substitute it in my head as I read. The ones that I have encountered that were intuitive and easy to understand were always like little mini functions, so I always wondered why they weren't just functions.

I can understand the need to define different build types for debug or cross platform builds in the preprocessor but the ability to define arbitrary substitutions seems to be useful only to make an already difficult language even more difficult to understand.

Why was such a complex preprocessor introduced for C? And does anyone have an example of using it that will make me understand why it still seems to be used for purposes other than simple if #debug style conditional compilations?

Edit:

Having read a number of answers I still just don't get it. The most common answer is to inline code. If the inline keyword doesn't do it then either it has a good reason to not do it, or the implementation needs fixing. I don't understand why a whole different mechanism is needed that means "really inline this code" (aside form the code being written before inline was around). I also don't understand the idea that was mentioned that "if its too silly to be put in a function". Surely any piece of code that takes an input and produces an output is best put in a function. I think I may not be getting it because I am not used to the micro optimisations of writing C, but the preprocessor just feels like a complex solution to a few simple problems.

解决方案

I end up having to remember what the macro is and substitute it in my head as I read.

That seems to reflect poorly on the naming of the macros. I would assume you wouldn't have to emulate the preprocessor if it were a log_function_entry() macro.

The ones that I have encountered that were intuitive and easy to understand were always like little mini functions, so I always wondered why they werent just functions.

Usually they should be, unless they need to operate on generic parameters.

#define max(a,b) ((a)<(b)?(b):(a))

will work on any type with an < operator.

More that just functions, macros let you perform operations using the symbols in the source file. That means you can create a new variable name, or reference the source file and line number the macro is on.

In C99, macros also allow you to call variadic functions such as printf

#define log_message(guard,format,...) \
   if (guard) printf("%s:%d: " format "\n", __FILE__, __LINE__,__VA_ARGS_);

log_message( foo == 7, "x %d", x)

In which the format works like printf. If the guard is true, it outputs the message along with the file and line number that printed the message. If it was a function call, it would not know the file and line you called it from, and using a vaprintf would be a bit more work.

这篇关于什么是C宏的有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆