What are C macros useful for?

前端 未结 18 2113
-上瘾入骨i
-上瘾入骨i 2020-11-28 19:45

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 complet

18条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 20:05

    Macros .. for when your &#(*$& compiler just refuses to inline something.

    That should be a motivational poster, no?

    In all seriousness, google preprocessor abuse (you may see a similar SO question as the #1 result). If I'm writing a macro that goes beyond the functionality of assert(), I usually try to see if my compiler would actually inline a similar function.

    Others will argue against using #if for conditional compilation .. they would rather you:

    if (RUNNING_ON_VALGRIND)
    

    rather than

    #if RUNNING_ON_VALGRIND
    

    .. for debugging purposes, since you can see the if() but not #if in a debugger. Then we dive into #ifdef vs #if.

    If its under 10 lines of code, try to inline it. If it can't be inlined, try to optimize it. If its too silly to be a function, make a macro.

提交回复
热议问题