学习 C 语言宏的建议
我已经选修了涵盖 C 语言的编程语言本科课程。然而,并不是说我已经开始在一家嵌入式系统公司工作,我看到 C 语言中经常使用大量的宏。
请分享一些链接,我可以从中了解更多信息宏。
我有 K&R 第二版,但我认为他们的风格太简洁了。有更多例子的东西对我来说会更好。
I have taken an undergrad course in programming Languages which covered C. However not that I have started working in a company in embedded systems, I see a plethora of macros being used regularly in C.
Please share some links from where I can learn more about macros.
I have K&R 2nd Ed, but I think their style is too terse. Something with more examples would be better for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
可能太明显了,但是关于 C 预处理器的 wiki 文章实际上非常好且详细:
http://en .wikipedia.org/wiki/C_preprocessor
Might be too obvious, but the wiki article on the C preprocessor is actually quite good and detailed:
http://en.wikipedia.org/wiki/C_preprocessor
非常稀缺。也就是说,你应该只将它们用于:常量
定义(代码中不应出现幻数),简单内联
函数(例如返回 bool 值的函数),仅此而已。
请记住,即使内联函数可以节省一些处理器周期,
它们通过膨胀代码并阻止所述函数的调试来弥补。
希望这对您有所帮助并回答您的问题。
be pretty scarce. That is to say, you should only use them for : constant
definitions ( no magic number should appear in the code ), simple inline
functions (for example ones that return a bool value), and that's pretty much it.
Remember that even though inline functions save you some processor cycles ,
they make up by bloating code and preventing the debugging of said functions.
Hope this helps and answers your question.
使用宏非常简单:
这是一个 MACRO_FUNCTION 计算乘积的示例。请注意,宏没有进行类型检查。它们只是在编译时被替换。因此,这取决于您使用正确的类型。
Using macro is pretty simple:
this is an example where MACRO_FUNCTION calculate product. Pay attention that macros are not type-checked. They are just substituted at compile time. So it's up to you use the right types .
老实说,现在您应该将宏的使用限制为非常简单的定义。
换句话说,不要浪费时间用宏来构建复杂的函数。
它们在过去往往有三个主要用途:
供预编译器使用的简单定义,例如
#define USE_DEBUG
和#ifdef USE_DEBUG
。总的来说,这些在可移植代码中仍然非常有价值。快速“内联”函数,例如
#define SQR(x) ((x) * (x))
,现在更适合真正的内联函数。宏版本有很多问题,其中之一是i = 7; j = SQR(i++);
不一定符合您的预期。预处理器枚举,如
#define OKAY 0
、#define ERR_NOMEM 1
等 - 这些最好作为真正的枚举来完成 - 因为宏版本是基本替换,您往往不会在调试信息中获取符号。In all honesty, you should limit your use of macros to very simple definitions nowadays.
In other words, don't waste your time crafting complex functions with macros.
They tended to have three main uses in the past:
simple definitions for the pre-compiler to use such as
#define USE_DEBUG
and#ifdef USE_DEBUG
. These are, by and large, stil very valuable in portable code.fast "inline" functions such as
#define SQR(x) ((x) * (x))
which are now much more suited to real inline functions. The macro version have a number of problems, one of which is thati = 7; j = SQR(i++);
will not necessarily do what you expect.pre-processor enumerations like
#define OKAY 0
,#define ERR_NOMEM 1
and so on - these are better done as real enumerations - because the macro versions are basic substitutions, you tend not to get the symbols in debugging information.这是关于 C 预处理器的两部分教程,名为“
使用预处理器的提示和技巧”,来自 IAR Systems:
Here is a two-part tutorial on the C preprocessor called "
Tips and tricks using the preprocesso" from IAR Systems:
您可以查看以下链接:
大多数技巧您必须自己掌握。也许如果您可以发布一些您不理解的宏,我们可以帮助您解决这些问题。
You can check out the following links:
Most of the tricks you'll have to pick up on your own. Maybe if you can post some of the macros you don't understand, we can help you out with those.