Variables C macro function











up vote
0
down vote

favorite












What's happening in this macro? I understand that #test expand this parameter to the literal text. But what does pre; and test; do?



#define MACRO_FN(test, pre, repeat, size)    
do {
printf("%s: ", #test);
for (int i = 0; i < repeat; i++) {
pre;
test;
}
} while (0)


This is used like so



MACRO_FN(a_func(an_array, size),, var1, size);


What do the double commas mean here?










share|improve this question




















  • 3




    Run the code through the preprocessor and look what the macro expands to.
    – Some programmer dude
    Nov 19 at 12:36






  • 1




    To omit pre;- maybe? Did you check the pre-processed code?
    – Sourav Ghosh
    Nov 19 at 12:37










  • I didn't think to check the output of the pre-processor, but I'll do that now
    – Jack Evans
    Nov 19 at 12:47






  • 2




    When invoking a preprocessor macro , "blank" is a valid argument; the call MACRO_FN(a,,b,c) means that pre will expand to blank
    – M.M
    Nov 19 at 12:50








  • 2




    Please note that macros like these should be avoided like the plague.
    – Lundin
    Nov 19 at 12:55















up vote
0
down vote

favorite












What's happening in this macro? I understand that #test expand this parameter to the literal text. But what does pre; and test; do?



#define MACRO_FN(test, pre, repeat, size)    
do {
printf("%s: ", #test);
for (int i = 0; i < repeat; i++) {
pre;
test;
}
} while (0)


This is used like so



MACRO_FN(a_func(an_array, size),, var1, size);


What do the double commas mean here?










share|improve this question




















  • 3




    Run the code through the preprocessor and look what the macro expands to.
    – Some programmer dude
    Nov 19 at 12:36






  • 1




    To omit pre;- maybe? Did you check the pre-processed code?
    – Sourav Ghosh
    Nov 19 at 12:37










  • I didn't think to check the output of the pre-processor, but I'll do that now
    – Jack Evans
    Nov 19 at 12:47






  • 2




    When invoking a preprocessor macro , "blank" is a valid argument; the call MACRO_FN(a,,b,c) means that pre will expand to blank
    – M.M
    Nov 19 at 12:50








  • 2




    Please note that macros like these should be avoided like the plague.
    – Lundin
    Nov 19 at 12:55













up vote
0
down vote

favorite









up vote
0
down vote

favorite











What's happening in this macro? I understand that #test expand this parameter to the literal text. But what does pre; and test; do?



#define MACRO_FN(test, pre, repeat, size)    
do {
printf("%s: ", #test);
for (int i = 0; i < repeat; i++) {
pre;
test;
}
} while (0)


This is used like so



MACRO_FN(a_func(an_array, size),, var1, size);


What do the double commas mean here?










share|improve this question















What's happening in this macro? I understand that #test expand this parameter to the literal text. But what does pre; and test; do?



#define MACRO_FN(test, pre, repeat, size)    
do {
printf("%s: ", #test);
for (int i = 0; i < repeat; i++) {
pre;
test;
}
} while (0)


This is used like so



MACRO_FN(a_func(an_array, size),, var1, size);


What do the double commas mean here?







c macros c-preprocessor






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 14:15

























asked Nov 19 at 12:34









Jack Evans

385




385








  • 3




    Run the code through the preprocessor and look what the macro expands to.
    – Some programmer dude
    Nov 19 at 12:36






  • 1




    To omit pre;- maybe? Did you check the pre-processed code?
    – Sourav Ghosh
    Nov 19 at 12:37










  • I didn't think to check the output of the pre-processor, but I'll do that now
    – Jack Evans
    Nov 19 at 12:47






  • 2




    When invoking a preprocessor macro , "blank" is a valid argument; the call MACRO_FN(a,,b,c) means that pre will expand to blank
    – M.M
    Nov 19 at 12:50








  • 2




    Please note that macros like these should be avoided like the plague.
    – Lundin
    Nov 19 at 12:55














  • 3




    Run the code through the preprocessor and look what the macro expands to.
    – Some programmer dude
    Nov 19 at 12:36






  • 1




    To omit pre;- maybe? Did you check the pre-processed code?
    – Sourav Ghosh
    Nov 19 at 12:37










  • I didn't think to check the output of the pre-processor, but I'll do that now
    – Jack Evans
    Nov 19 at 12:47






  • 2




    When invoking a preprocessor macro , "blank" is a valid argument; the call MACRO_FN(a,,b,c) means that pre will expand to blank
    – M.M
    Nov 19 at 12:50








  • 2




    Please note that macros like these should be avoided like the plague.
    – Lundin
    Nov 19 at 12:55








3




3




Run the code through the preprocessor and look what the macro expands to.
– Some programmer dude
Nov 19 at 12:36




Run the code through the preprocessor and look what the macro expands to.
– Some programmer dude
Nov 19 at 12:36




1




1




To omit pre;- maybe? Did you check the pre-processed code?
– Sourav Ghosh
Nov 19 at 12:37




To omit pre;- maybe? Did you check the pre-processed code?
– Sourav Ghosh
Nov 19 at 12:37












I didn't think to check the output of the pre-processor, but I'll do that now
– Jack Evans
Nov 19 at 12:47




I didn't think to check the output of the pre-processor, but I'll do that now
– Jack Evans
Nov 19 at 12:47




2




2




When invoking a preprocessor macro , "blank" is a valid argument; the call MACRO_FN(a,,b,c) means that pre will expand to blank
– M.M
Nov 19 at 12:50






When invoking a preprocessor macro , "blank" is a valid argument; the call MACRO_FN(a,,b,c) means that pre will expand to blank
– M.M
Nov 19 at 12:50






2




2




Please note that macros like these should be avoided like the plague.
– Lundin
Nov 19 at 12:55




Please note that macros like these should be avoided like the plague.
– Lundin
Nov 19 at 12:55












2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










Here is a minimal example:



#define repeat 5    // I added this, because 'repeat' is not mentionned in your question

#define MACRO_FN(test, pre, var1, size)
do {
printf("%s: ", #test);
for (int i = 0; i < repeat; i++) {
pre;
test;
}
} while (0)

void foo()
{
}

void func(int a, int b)
{
}

int main()
{
MACRO_FN(func(2, 3), foo(), var1, size);
}


Once preprocessed, the code is equivalent to this:



int main()
{
printf("%s: ", "func(2,3)");
for (int i = 0; i < 5; i++)
{
foo();
func(2, 3);
}
}


So that macro is a wrapper that prints the function name plus it's parameters as it is invoked with the macro and executes that function specified in the first parameter repeat times (whatever repeat is). If the second parameter is omitted, the function that has that name is simple not invoked before the function mentioned before as in the following example:



int main()
{
MACRO_FN(func(2, 3),, var1, size);
}


Once preprocessed, the code is equivalent to this:



int main()
{
printf("%s: ", "func(2,3)");
for (int i = 0; i < 5; i++)
{
;
func(2, 3);
}
}


Note:



I removed the do while(0) from the equivalent programs for brevity, read this SO article for more information:






share|improve this answer






























    up vote
    3
    down vote













    pre and test seem to be two functions.
    Based on how it is written, we can guess that pre is a function called before the test.



    The double comma has no special meaning. It is just here because the second parameter (pre) was omitted.



    Edit: As a side note that kind of macro "should be avoided like plague", as @Lundin put it.






    share|improve this answer










    New contributor




    Xaxetrov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.


















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374784%2fvariables-c-macro-function%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted










      Here is a minimal example:



      #define repeat 5    // I added this, because 'repeat' is not mentionned in your question

      #define MACRO_FN(test, pre, var1, size)
      do {
      printf("%s: ", #test);
      for (int i = 0; i < repeat; i++) {
      pre;
      test;
      }
      } while (0)

      void foo()
      {
      }

      void func(int a, int b)
      {
      }

      int main()
      {
      MACRO_FN(func(2, 3), foo(), var1, size);
      }


      Once preprocessed, the code is equivalent to this:



      int main()
      {
      printf("%s: ", "func(2,3)");
      for (int i = 0; i < 5; i++)
      {
      foo();
      func(2, 3);
      }
      }


      So that macro is a wrapper that prints the function name plus it's parameters as it is invoked with the macro and executes that function specified in the first parameter repeat times (whatever repeat is). If the second parameter is omitted, the function that has that name is simple not invoked before the function mentioned before as in the following example:



      int main()
      {
      MACRO_FN(func(2, 3),, var1, size);
      }


      Once preprocessed, the code is equivalent to this:



      int main()
      {
      printf("%s: ", "func(2,3)");
      for (int i = 0; i < 5; i++)
      {
      ;
      func(2, 3);
      }
      }


      Note:



      I removed the do while(0) from the equivalent programs for brevity, read this SO article for more information:






      share|improve this answer



























        up vote
        3
        down vote



        accepted










        Here is a minimal example:



        #define repeat 5    // I added this, because 'repeat' is not mentionned in your question

        #define MACRO_FN(test, pre, var1, size)
        do {
        printf("%s: ", #test);
        for (int i = 0; i < repeat; i++) {
        pre;
        test;
        }
        } while (0)

        void foo()
        {
        }

        void func(int a, int b)
        {
        }

        int main()
        {
        MACRO_FN(func(2, 3), foo(), var1, size);
        }


        Once preprocessed, the code is equivalent to this:



        int main()
        {
        printf("%s: ", "func(2,3)");
        for (int i = 0; i < 5; i++)
        {
        foo();
        func(2, 3);
        }
        }


        So that macro is a wrapper that prints the function name plus it's parameters as it is invoked with the macro and executes that function specified in the first parameter repeat times (whatever repeat is). If the second parameter is omitted, the function that has that name is simple not invoked before the function mentioned before as in the following example:



        int main()
        {
        MACRO_FN(func(2, 3),, var1, size);
        }


        Once preprocessed, the code is equivalent to this:



        int main()
        {
        printf("%s: ", "func(2,3)");
        for (int i = 0; i < 5; i++)
        {
        ;
        func(2, 3);
        }
        }


        Note:



        I removed the do while(0) from the equivalent programs for brevity, read this SO article for more information:






        share|improve this answer

























          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          Here is a minimal example:



          #define repeat 5    // I added this, because 'repeat' is not mentionned in your question

          #define MACRO_FN(test, pre, var1, size)
          do {
          printf("%s: ", #test);
          for (int i = 0; i < repeat; i++) {
          pre;
          test;
          }
          } while (0)

          void foo()
          {
          }

          void func(int a, int b)
          {
          }

          int main()
          {
          MACRO_FN(func(2, 3), foo(), var1, size);
          }


          Once preprocessed, the code is equivalent to this:



          int main()
          {
          printf("%s: ", "func(2,3)");
          for (int i = 0; i < 5; i++)
          {
          foo();
          func(2, 3);
          }
          }


          So that macro is a wrapper that prints the function name plus it's parameters as it is invoked with the macro and executes that function specified in the first parameter repeat times (whatever repeat is). If the second parameter is omitted, the function that has that name is simple not invoked before the function mentioned before as in the following example:



          int main()
          {
          MACRO_FN(func(2, 3),, var1, size);
          }


          Once preprocessed, the code is equivalent to this:



          int main()
          {
          printf("%s: ", "func(2,3)");
          for (int i = 0; i < 5; i++)
          {
          ;
          func(2, 3);
          }
          }


          Note:



          I removed the do while(0) from the equivalent programs for brevity, read this SO article for more information:






          share|improve this answer














          Here is a minimal example:



          #define repeat 5    // I added this, because 'repeat' is not mentionned in your question

          #define MACRO_FN(test, pre, var1, size)
          do {
          printf("%s: ", #test);
          for (int i = 0; i < repeat; i++) {
          pre;
          test;
          }
          } while (0)

          void foo()
          {
          }

          void func(int a, int b)
          {
          }

          int main()
          {
          MACRO_FN(func(2, 3), foo(), var1, size);
          }


          Once preprocessed, the code is equivalent to this:



          int main()
          {
          printf("%s: ", "func(2,3)");
          for (int i = 0; i < 5; i++)
          {
          foo();
          func(2, 3);
          }
          }


          So that macro is a wrapper that prints the function name plus it's parameters as it is invoked with the macro and executes that function specified in the first parameter repeat times (whatever repeat is). If the second parameter is omitted, the function that has that name is simple not invoked before the function mentioned before as in the following example:



          int main()
          {
          MACRO_FN(func(2, 3),, var1, size);
          }


          Once preprocessed, the code is equivalent to this:



          int main()
          {
          printf("%s: ", "func(2,3)");
          for (int i = 0; i < 5; i++)
          {
          ;
          func(2, 3);
          }
          }


          Note:



          I removed the do while(0) from the equivalent programs for brevity, read this SO article for more information:







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 at 15:47

























          answered Nov 19 at 14:11









          Jabberwocky

          26.2k93769




          26.2k93769
























              up vote
              3
              down vote













              pre and test seem to be two functions.
              Based on how it is written, we can guess that pre is a function called before the test.



              The double comma has no special meaning. It is just here because the second parameter (pre) was omitted.



              Edit: As a side note that kind of macro "should be avoided like plague", as @Lundin put it.






              share|improve this answer










              New contributor




              Xaxetrov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






















                up vote
                3
                down vote













                pre and test seem to be two functions.
                Based on how it is written, we can guess that pre is a function called before the test.



                The double comma has no special meaning. It is just here because the second parameter (pre) was omitted.



                Edit: As a side note that kind of macro "should be avoided like plague", as @Lundin put it.






                share|improve this answer










                New contributor




                Xaxetrov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.




















                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  pre and test seem to be two functions.
                  Based on how it is written, we can guess that pre is a function called before the test.



                  The double comma has no special meaning. It is just here because the second parameter (pre) was omitted.



                  Edit: As a side note that kind of macro "should be avoided like plague", as @Lundin put it.






                  share|improve this answer










                  New contributor




                  Xaxetrov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  pre and test seem to be two functions.
                  Based on how it is written, we can guess that pre is a function called before the test.



                  The double comma has no special meaning. It is just here because the second parameter (pre) was omitted.



                  Edit: As a side note that kind of macro "should be avoided like plague", as @Lundin put it.







                  share|improve this answer










                  New contributor




                  Xaxetrov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer








                  edited Nov 19 at 13:22





















                  New contributor




                  Xaxetrov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered Nov 19 at 12:42









                  Xaxetrov

                  464




                  464




                  New contributor




                  Xaxetrov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Xaxetrov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Xaxetrov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374784%2fvariables-c-macro-function%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Wiesbaden

                      Marschland

                      Dieringhausen