0

I know that you can define a function like this:

#PRINT printf("hello world\n");

Is it possible, instead, to define a function like this?

#PRINT printf("hello world\n"), printf("hello stack\n");

(... where the function has two steps to it.) Is this possible?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
kpo8
  • 59
  • 1
  • 7
  • 1
    http://c-faq.com/cpp/multistmt.html – jamesdlin Apr 07 '19 at 23:56
  • 2
    That doesn't look like C. – Raymond Chen Apr 08 '19 at 02:45
  • 1
    Are you thinking of `#define PRINT printf("hello world\n")` etc? Did you try using the second version? What happened? Have you learned about the comma operator? It's doable, though whether it's a good idea is a separate discussion. See also [Why use apparently meaningless `do … while (0)` and `if`…`else` statements in macros?](https://stackoverflow.com/questions/154136/why-use-apparently-meaningless-do-while-and-if-else-statements-in-macros) Consider using `inline` (better, `static inline`) functions rather than macros. Note that you should not normally end a `#define` with a semicolon. – Jonathan Leffler Apr 08 '19 at 05:52

1 Answers1

2

Thanks for the above comments. I figured out the answer to my question.

To define a macro with multiple steps you need to do this

    #define FOO {\
                     printf("hello world\n");\
                     printf("hello stack\n");\
                 }

Calling FOO will then execute those two print statements.

kpo8
  • 59
  • 1
  • 7