1

Nonetheless, macros are valuable. One practical example comes from <stdio.h>, in which getchar and putchar are often defined as macros to avoid the run-time overhead of a function call per character processed. The functions in <ctype.h> are also usually implemented as macros.

This is from the book. I did not understand why MACROS help reducing overhead. All they are doing is to just increase one more step in compilation process.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
smartnerd
  • 85
  • 6
  • Yeah I know. What about overhead? How does it reduces overhead for a program? Are MACROS called in a different way than other programs? Are the compiled separately? – smartnerd Jun 01 '17 at 01:19
  • 1
    Macros are replaced directly in the source code, so there is no function call at runtime. Compilers may automatically inline functions more these days than when K&R was written, making this point somewhat less important. This is one reason not to learn from outdated (if remarkable) books. – ad absurdum Jun 01 '17 at 01:22
  • 2
    To expand on what David said, each function call takes a bit of time. I would still recommend reading K&R though. C has moved on since 1990, but not _that_ much. It's not too difficult to learn what's changed after finishing the book. – yellowantphil Jun 01 '17 at 01:25
  • @yellowantphil Thanks a lot! Any other sources to catch up with latest changes and programming techniques in C? – smartnerd Jun 01 '17 at 01:31
  • @yellowantphil-- I certainly don't mean to disparage K&R, and I agree that it is worth one or two reads. But, I am not sure that it is the best book to learn C from the first time anymore. Reasonable opinions may vary on this point. For OP: [here is a list of good C books maintained on SO](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – ad absurdum Jun 01 '17 at 01:37
  • Thanks @David Bowling – smartnerd Jun 01 '17 at 01:41
  • @smartnerd I'm probably not the best person to ask about updates, but the C99 and C11 Wikipedia articles have an overview of the changes. One thing that I didn't notice highlighted on the C99 Wikipedia page is that C99 and C11 allow syntax like `for (int i = 0; i < 10; i++) { doStuff(i) };`, which does what you probably expect (makes `i` local to the `for` loop). I've never used any C11 features myself. – yellowantphil Jun 01 '17 at 01:43
  • 1
    I read K&R a few times, its dated, but not that much. What they mean by overhead is that when you call a function is C, the system uses extra recourses. It makes copy of whatever you send to the function, and puts it in the stack. When you use a macro, it just copies whatever is in the macro in place. So no extra resources are used. That true today as it was when they wrote there book. I read the 2nd ed. First is more dated. – Manny_Mar Jun 01 '17 at 01:45
  • @smartnerd-- you can always [go straight to the source and read the Standard](http://port70.net/~nsz/c/c11/n1570.html#Contents) to learn about what is current in C11. Once you get used to finding answers here, this becomes invaluable. And participating on SO will help too :) – ad absurdum Jun 01 '17 at 01:49
  • Also K&R explain pointer really well. Other sources sometimes give misleading explanations to simplify things for someone learning. You find many people who say they only understood pointers after reading K&R (https://stackoverflow.com/questions/4025768/what-do-people-find-difficult-about-c-pointers) You find many people online saying the same thing. – Manny_Mar Jun 01 '17 at 02:03

1 Answers1

1

A macro here is just an old version of inlining a function, automating the process of copy-pasting the function body into every use. Good reading:

https://en.wikipedia.org/wiki/Inline_function

Sam Pagenkopf
  • 258
  • 1
  • 5