NO

Author Topic: Variable Argument Macro  (Read 8178 times)

CCoder

  • Guest
Variable Argument Macro
« on: January 11, 2008, 12:30:09 AM »
I may be in the wrong place but if this needs to be moved...

I want to write a macro with a variable number of arguments (4-?) something like
MACRO_TEST(VAR,count,4,HEX,test,8,CHAR)  etc.  This would cause something like  count=0x0000   test="Eureka!" to be printed in a logging file I am building.  The variable name, length and type of printing to be done could be repeated as many times as necessary.

I have done a few searches on the web and found that __VA_ARGS__ appears to allow you to create a macro with a variable number of arguments as a string ("count,4,HEX,test,8,CHAR").  This is an ISO C99 Standard that Pelles C appears to support with the caution that "(for stuff listed as "supported" it sometimes just mean "accept the new syntax" without doing much else)". 

I tried compiling the following macro:
270 #define SQLLIB_TEST(dumpRecd,...)       \
271   fprintf(stdout,"%s\n",dumpRecd);         \
272   fprintf(stdout,"test\n");               \
273   fprintf(stdout,"%s",__VA_ARGS__);

and all I get from the compile is the message "C:\My_C_Library\Include\mysqllib_client.h(270): error #1049: Syntax error in macro parameters."

Is the use of the __VA_ARGS__ macro "exploited" in Pelles C (I.E Full support for variable macros)?
If so where could I find a working example with __VA_ARGS__?
If not, any thoughts on how I could accomplish my goal via some other method (variable argument functions???) are greatly appreciated....

THANKS!

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Variable Argument Macro
« Reply #1 on: January 11, 2008, 08:25:11 AM »
Yes, it is supported (only a few cases can accept a keyword without doing anything, so assume it is supported...)

This is the simplest of samples:
Code: [Select]
#include <stdio.h>

#define LogFile(...)  (void)fprintf(stderr, __VA_ARGS__)

int main(void)
{
    LogFile("Test\n");
    LogFile("Line = %d, index = %d\n", __LINE__, -1);

    return 0;
}

I can't tell from your post how the macro is used, so impossible to say why it's not working in your case...
/Pelle

severach

  • Guest
Re: Variable Argument Macro
« Reply #2 on: January 23, 2008, 03:52:18 AM »
I want to write a macro with a variable number of arguments (4-?) something like
MACRO_TEST(VAR,count,4,HEX,test,8,CHAR)  etc.  This would cause something like  count=0x0000   test="Eureka!" to be printed in a logging file I am building.  The variable name, length and type of printing to be done could be repeated as many times as necessary.
While people competing in the IOCCC have made C preprocessors do amazing things, the normal usage of variable args macros doesn't have the muscle to handle this problem. You can do it as a function because you can program the function to detect the end of the arg list in a manner you specify then wrap it in a macro if you want to. I have rearranged your parameters to place a zero at the end which code yet to be written could detect.
MACRO_TEST(4,count,HEX,8,test,CHAR,0);

Unfortunately your macro does little more than printf() already does. Perhaps printf() could fill the bill.

Quote
I have done a few searches on the web and found that __VA_ARGS__ appears to allow you to create a macro with a variable number of arguments as a string ("count,4,HEX,test,8,CHAR").
It is not created as a string. It is the list of arguments in compilable form. You can make __VA_ARGS__ a string with the # macro stringize operator as shown in the Pelles-C help. Search the Index for "C Preprocessor" then click on #define.

Hammurabi

  • Guest
Re: Variable Argument Macro
« Reply #3 on: March 06, 2010, 12:07:13 PM »
Sorry to bump this old post, but I got the same error message as OP. In my case, the problem was due to including a common header file (where the macro using __VA_ARGS__ is defined) in the resource file (.rc). After lots of unsuccessful tries, I realized the message was being printed out by the resource compiler (porc), not pocc. The solution to this is to prevent that macro from being processed by porc.
« Last Edit: March 06, 2010, 12:08:53 PM by Hammurabi »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Variable Argument Macro
« Reply #4 on: March 06, 2010, 05:52:12 PM »
You can use porc/rc predefined macro RC_INVOKED in header file to prevent that include file where __VA_ARGS__ is used.

Code: [Select]
#ifndef RC_INVOKED
#include <foo.h>
#endif //RC_INVOKED
May the source be with you