Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > macro with variable length of argument

Reply
Thread Tools

macro with variable length of argument

 
 
ethan
Guest
Posts: n/a
 
      09-25-2005
Hi All,

How to write a macro with variable length of argument?
For example, if i need a macro to check return value of printf.

#define PRINTF_ESC(x, ...) if(printf(x, ...) == 10) goto end;

However, it doesn't work. >_<

Thank you very much.

Ethan

 
Reply With Quote
 
 
 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      09-25-2005
ethan wrote on 25/09/05 :
> How to write a macro with variable length of argument?


C90 :

#define PRINTF(a) print a

usage :

PRINTF (("Hi, I'm %d\n", 49));

> For example, if i need a macro to check return value of printf.
>
> #define PRINTF_ESC(x, ...) if(printf(x, ...) == 10) goto end;
>
> However, it doesn't work. >_<


It may works with C99. I'm not sure of the syntax...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair


 
Reply With Quote
 
 
 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      09-25-2005
(supersedes <>)

ethan wrote on 25/09/05 :
> How to write a macro with variable length of argument?


C90 :

#define PRINTF(a) printf a

usage :

PRINTF (("Hi, I'm %d\n", 49));

> For example, if i need a macro to check return value of printf.
>
> #define PRINTF_ESC(x, ...) if(printf(x, ...) == 10) goto end;
>
> However, it doesn't work. >_<


It may works with C99. I'm not sure of the syntax...


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."


 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      09-25-2005
"ethan" <> wrote:
# Hi All,
#
# How to write a macro with variable length of argument?
# For example, if i need a macro to check return value of printf.
#
# #define PRINTF_ESC(x, ...) if(printf(x, ...) == 10) goto end;

You can do it with double parentheses. (Note no parentheses
after printf.)

@ more t.c
#define PRINTF_ESC(x) if(printf x == 10) goto end;
PRINTF_ESC((a))
PRINTF_ESC((a,b,c,d))

@ cc -E t.c
# 1 "t.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "t.c"

if(printf (a) == 10) goto end;
if(printf (a,b,c,d) == 10) goto end;


I think gcc has an extension for this, but it might not
be available elsewhere.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
OOOOOOOOOO! NAVY SEALS!
 
Reply With Quote
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      09-25-2005
ethan wrote:
> How to write a macro with variable length of argument?


Don't.

> For example, if i need a macro to check return value of printf.


Then you write a function doing that, somewhere along these lines:

void foo( char const* fmt, ...)
{
va_list args;
va_start( args, fmt);
if(42 != vprintf( fmt, args))
exit(667);
}

Macros are evil.

Uli

 
Reply With Quote
 
Charles Mills
Guest
Posts: n/a
 
      09-25-2005

ethan wrote:
> Hi All,
>
> How to write a macro with variable length of argument?
> For example, if i need a macro to check return value of printf.
>
> #define PRINTF_ESC(x, ...) if(printf(x, ...) == 10) goto end;
>
> However, it doesn't work. >_<
>
> Thank you very much.
>
> Ethan


In C99 you can do the following:

#define PRINTF_ESC(x, ...) if(printf(x, __VA_ARGS__) == 10) goto end;

The only problem with this is that PRINTF_ESC will require two or more
parameters, since the elispses must hold one or more parameters. A
better way to do this would be:

#define PRINTF_ESC(...) if(printf(__VA_ARGS__) == 10) goto end;

FYI: it is bad to use goto's (especially in a macro definition!) and
customary to leave the semicolon off the end of you macro definition or
to do the following:
#define PRINTF_ESC(...) do { if(printf(__VA_ARGS__) == 10) goto end; }
while (0)

Also note that the meaning of the elipses in macro functions is much
different than in real functions and as another poster mentions your
better off using a real function for this (that way you can't use a
goto .

Also see http://www.delorie.com/gnu/docs/gcc/cpp_19.html

-Charlie

 
Reply With Quote
 
Stan Milam
Guest
Posts: n/a
 
      09-25-2005
Ulrich Eckhardt wrote:
> Macros are evil.
>
> Uli
>


Why?
 
Reply With Quote
 
Skarmander
Guest
Posts: n/a
 
      09-25-2005
Stan Milam wrote:
> Ulrich Eckhardt wrote:
>
>> Macros are evil.
>>

>
> Why?


If ya hafta ask, ye'll never know!

That said, "macros are evil" is primarily a cry from the C++ camp, see
for example
http://www.parashift.com/c++-faq-lit...s.html#faq-9.5

While preferring inline functions and constants to macros is good
advice, C programmers can't and won't do it. Can't because, before C99
(which will take another, oh, ten years or so to catch on), there was no
standard mechanism for inlining functions, won't because it's a staple
of C culture.

The use of #define for symbolic constants is so ingrained that it will
probably remain part of C until the time_t's wrap around, but luckily
their use is disciplined enough not to cause much trouble... usually.
Ditto on include guards: #ifndef HEADER_H #define HEADER_H... Go beyond
that and it quickly goes downhill.

The preprocessor unlocks power programmers are all too eager to (ab)use.
More than a few times I've stared at a jungle of ALL-CAPS code where
"convenient" macros effectively defined a completely new language
because the perpetrators either loathed typing or apparently believed
that by hiding crud behind macros, it stops being crud. When you have to
tell your compiler to give you the preprocessor output to pore over
because you're tired of chasing include files for the definitions, you
know you're having a bad day.

S.
 
Reply With Quote
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      09-25-2005
Skarmander wrote:
> That said, "macros are evil" is primarily a cry from the C++ camp, see
> for example
> http://www.parashift.com/c++-faq-lit...s.html#faq-9.5


Hmmm, I don't think there is anything that only applies to C++ but several
reasons are given in that FAQ so it could be interesting reading even for
a C programmer. Multiple evaluation of side-effects, the absolute
disrespect for scoping rules, the fact that they can be replaced with
inline functions (in many cases) are those that immediately jump to my
mind.

> While preferring inline functions and constants to macros is good
> advice, C programmers can't and won't do it. Can't because, before C99
> (which will take another, oh, ten years or so to catch on), there was no
> standard mechanism for inlining functions, won't because it's a staple
> of C culture.


I hope I misunderstand you here, or are you really saying that macro
functions are part of C culture and therefore you are using them even if
there are better alternatives?

Anyhow, I never found a compiler that did not understand inline (yes, a few
of them needed __inline or __inline__, a case where the preprocessor can
be made good use of). I admit I don't do much programming on legacy
systems though, so it might be possible that such compilers still exist.
None of the compilers I did and do C with claimed C99 conformance btw,
except for newer gccs perhaps.

> The use of #define for symbolic constants is so ingrained that it will
> probably remain part of C until the time_t's wrap around, but luckily
> their use is disciplined enough not to cause much trouble... usually.


AFAIK, C has another big problem in that aspect, a constant does not
constitute an 'integral constant expression', i.e. it can't be used
specify the size of an array. I'm rather sure of this for C89, not so sure
about C99.

Hmm, maybe this is the reason you attribute 'macros are evil' to C++ since
in C++ 'inline' is part of the language and constants can be used to
declare arrays, so two possible reasons that you need them are gone.


> Ditto on include guards: #ifndef HEADER_H #define HEADER_H... Go beyond
> that and it quickly goes downhill.


Those are the best present solution for something that should be a proper
part of the language. It doesn't justify using macros in places where a
better alternative exists.

> The preprocessor unlocks power programmers are all too eager to (ab)use.
> More than a few times I've stared at a jungle of ALL-CAPS code where
> "convenient" macros effectively defined a completely new language
> because the perpetrators either loathed typing or apparently believed
> that by hiding crud behind macros, it stops being crud. When you have to
> tell your compiler to give you the preprocessor output to pore over
> because you're tired of chasing include files for the definitions, you
> know you're having a bad day.


My heart feels with you. I've been refactoring someone else's code for the
last three weeks and will probably go on with it this week. This code
didn't contain an overabundance of macros but showed a serious
misunderstanding of how to use a typed language and OO design. It's not
even C FWIW.

Uli

 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      09-25-2005
Ulrich Eckhardt <> wrote:
# ethan wrote:
# > How to write a macro with variable length of argument?
#
# Don't.

You can't use goto from one function to another.
You would have to set up a longjmp.

# Then you write a function doing that, somewhere along these lines:
#
# void foo( char const* fmt, ...)
# {
# va_list args;
# va_start( args, fmt);
# if(42 != vprintf( fmt, args))
# exit(667);

It used a goto, not an exit.


--
SM Ryan http://www.rawbw.com/~wyrmwif/
What kind of convenience store do you run here?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable argument function as a parameter of a variable argument function AikidoGuy C Programming 11 11-21-2011 10:43 PM
Variable Length Argument List with Tomcat/JSP fails to compile. smm1@mail.com Java 3 12-12-2006 05:47 AM
Function pointers, variable argument functions calling other variable-argument functions (sort of) S?ren Gammelmark C Programming 1 01-07-2005 09:41 PM
How to pass variable argument list to another function w/ variable argument list? Ben Kial C Programming 1 11-15-2004 01:51 AM
transfering variable length argument list ANDERS FLODERUS C++ 1 12-20-2003 08:27 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57