Using and returning output in C macro

后端 未结 5 1773
说谎
说谎 2021-02-02 12:33

I\'m trying to instrument some code to catch and print error messages. Currently I\'m using a macro somethng like this:

#define my_function(x) \\
  switch(functi         


        
5条回答
  •  我在风中等你
    2021-02-02 12:41

    A very late reply. But none the less. I agree inline functions are better but MACROs do offer some pretty printing fun you can't get with inline functions. I agree with @qrdl that you can indeed use statement expressions had you restructured your statements a bit. Here is how it would work with a macro -

    #define my_function(x, y) ({ \
      int __err = 0; \
      do { \
        __err = function(x, y); \
        switch(__err) { \
          case ERROR: \
            fprintf(stderr, "Error!\n"); \
            break; \
        } \
      } while(0); \
      __err; \
    })
    

提交回复
热议问题