title: Is it OK to use a code block as an argument for a C macro?
body: <p>I have a pattern that is basically some boilerplate code with a part that varies in the middle</p>
  
  <pre><code>if(condition){
      struct Foo m = start_stuff();
      { m.foo = bar(1,2); m.baz = 17; } //this part varies
      end_stuff();
  }
  </code></pre>
  
  <p>Is it OK to make a macro taht takes that intermediate code block as an argument? The rules for macro expansion in C seem awfully complicated so I am not sure if there aren't any corner cases that could come and bite me in the future (in particular, I don't understand how the macro arguments are separated if my code has commas in it).</p>
  
  <pre><code>#define MY_MACRO(typ, do_stuff) do { \
      if(condition){ \
          struct typ m = start_stuff(); \
          do_stuff; \
          end_stuff(); \
      } \
  }while(0)
  
  //usage
  MY_MACRO(Foo, {
     m.foo = bar(1,2);
     m.baz = 17;
  });
  </code></pre>
  
  <p>So far the only thing that I managed to think of is <code>break</code> and <code>continue</code> getting captured if I use looping statements in my macro and that would be an acceptable tradeoff for my particular use case.</p>
  
  <p><strong>edit:</strong> Of course, I would have used a functions if I could. The example I used in this question is simplified and doesn't showcase the bits that can only work with macro magic.</p>