Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calling function with macro in C

I have function in C with 3 params. I want to call that function with pre-defined values. Is there any solution?

My idea which does not work.

#define PRE_DEFINED_MACRO   (10, 15 , true)

void myFunc(uint8_t temp, uint32_t value, bool valid)
{
  .... 
}
....
//call the function like that
myFunc(PRE_DEFINED_MACRO);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your macro already includes parentheses, so when this …

myFunc(PRE_DEFINED_MACRO);

… is expanded, the result is:

myFunc((10, 15 , true));

You could do this instead:

myFunc PRE_DEFINED_MACRO;

I find that pretty confusing, however. Personally, I would prefer to remove the parentheses from the macro’s replacement text:

#define MYFUNC_ARGS  10, 15 , true

// ...

myFunc(MYFUNC_ARGS);

That makes it much clearer that the call to myFunc() is, in fact, a function call. The choice of macro name also makes the intended purpose of the macro much clearer.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading