开发者

Suggestions for learning about macros in C

开发者 https://www.devze.com 2023-02-26 10:14 出处:网络
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

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开发者_如何学C is too terse. Something with more examples would be better for me.


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 that i = 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.


Here is a two-part tutorial on the C preprocessor called " Tips and tricks using the preprocesso" from IAR Systems:

  • Basics of Using the Preprocessor
  • Advanced Preprocessor Tips and Tricks


You can check out the following links:

  • http://www.delorie.com/gnu/docs/gcc/cpp_16.html
  • http://www.cprogramming.com/tutorial/cpreprocessor.html

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.


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


Hello,
As far as my knowledge goes the use of macro defs in embedded systems should 

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.


Using macro is pretty simple:

#define MACRO_FUNCTION(par1, par2) (par1 * par2)
int product = MACRO_FUNCTION(4 ,5);

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 .

0

精彩评论

暂无评论...
验证码 换一张
取 消