Pelles C forum

C language => Beginner questions => Topic started by: pber on April 13, 2015, 01:52:54 AM

Title: about macros
Post by: pber on April 13, 2015, 01:52:54 AM
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
Title: Re: about macros
Post by: TimoVJL on April 13, 2015, 06:19:20 AM
do you mean __VA_ARGS__ ?
Title: Re: about macros
Post by: Snowman on April 13, 2015, 09:49:35 AM
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 (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__)
Title: Re: about macros
Post by: pber on April 13, 2015, 07:02:39 PM
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...

Title: Re: about macros
Post by: Snowman on April 13, 2015, 07:12:05 PM
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);
}
Title: Re: about macros
Post by: pber on April 13, 2015, 07:23:01 PM
oh gosh.... it was so simple...
many thanks