News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

about macros

Started by pber, April 13, 2015, 01:52:54 AM

Previous topic - Next topic

pber

hi all,
i'm trying to switch from gcc,
mainly because PellesC it's very fast on compiling.

Does exist a way, in PellesC, to emulate the variadic macros of gcc?

thanks
paolo

TimoVJL

do you mean __VA_ARGS__ ?
May the source be with you

Snowman

Quote from: pber on April 13, 2015, 01:52:54 AM
Does exist a way, in PellesC, to emulate the variadic macros of gcc?

http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html

Variadic macros are part of the C99 standard, which Pelles C supports.
So you don't need to "emulate" them as if they're an extension to C.
Example:

Quote#define eprintf(format, ...) fprintf (stderr, format, __VA_ARGS__)

pber

thanks, now I see the issue:

gcc allows to pass no arguments in place of <<...>>
i.e.: given
#define f(__fmt, ...) printf(__fmt, __VA_ARGS__)

you can use it with:
f("just a message\n");

but in case PellesC says:
Disagreement in number of macro arguments.

I cant' figure out how to deal with this...


Snowman

You can simply allow the ellipses (...) to include __fmt, then you can have one argument.

Example:
#include <stdio.h>

#define EPRINTF(...)    fprintf(stderr, __VA_ARGS__)

int main(void)
{
    EPRINTF("test 1\n");
    EPRINTF("%s %d\n", "test", 2);
}

pber

oh gosh.... it was so simple...
many thanks