What are C macros useful for?

前端 未结 18 2078
-上瘾入骨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:22

    They are a programming language (a simpler one) on top of C, so they are useful for doing metaprogramming in compile time... in other words, you can write macro code that generates C code in less lines and time that it will take writing it directly in C.

    They are also very useful to write "function like" expressions that are "polymorphic" or "overloaded"; e.g. a max macro defined as:

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

    is useful for any numeric type; and in C you could not write:

    int max(int a, int b) {return a>b?a:b;}
    float max(float a, float b) {return a>b?a:b;}
    double max(double a, double b) {return a>b?a:b;}
    ...
    

    even if you wanted, because you cannot overload functions.

    And not to mention conditional compiling and file including (that are also part of the macro language)...

提交回复
热议问题