NO

Author Topic: about macros  (Read 3191 times)

pber

  • Guest
about macros
« 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

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: about macros
« Reply #1 on: April 13, 2015, 06:19:20 AM »
do you mean __VA_ARGS__ ?
May the source be with you

Snowman

  • Guest
Re: about macros
« Reply #2 on: April 13, 2015, 09:49:35 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

  • Guest
Re: about macros
« Reply #3 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
Code: [Select]
#define f(__fmt, ...) printf(__fmt, __VA_ARGS__)
you can use it with:
Code: [Select]
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

  • Guest
Re: about macros
« Reply #4 on: April 13, 2015, 07:12:05 PM »
You can simply allow the ellipses (...) to include __fmt, then you can have one argument.

Example:
Code: [Select]
#include <stdio.h>

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

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

pber

  • Guest
Re: about macros
« Reply #5 on: April 13, 2015, 07:23:01 PM »
oh gosh.... it was so simple...
many thanks