By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

macro doubt

P: n/a
is it possible to pass variable no.of arguments to a macro ...

#define print(a,b) printf(a,b)

void main()
{
int a=1,b=2,c=3;
print("%d %d %d",(a,b,c));
}

the problem in the above code is the expression (a,b,c) evaluates to c
,
and only the value of c will get printed.

Dec 23 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
sinbad said:
is it possible to pass variable no.of arguments to a macro ...
Not in standard C as currently implemented. If you are fortunate and unique
enough to have a C99 compiler, use ... in the macro text and __VA_ARGS__ in
the replacement.
#define print(a,b) printf(a,b)

void main()


main returns int.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 23 '05 #2

P: n/a
sinbad wrote:
is it possible to pass variable no.of arguments to a macro ...
The standard adopted in 1999 provides for this; the old standard from
1989(1990) does not. Some compilers which otherwise conform to the
1989(1990) standard have mechanisms for this; some look like the 1999
standard mechanism and might be portable to a C99 implementation, but
others use syntax or semantics differing from those adopted in the 199
standard. So use these with care, reading the documentation carefully,
and if you expect ever to use a 1989(1990) compiler with your code, then
use them not at all.

But ...
void main()

^^^^
*NO* standard version of C (or the pre-standard K&R C) allows this for
programs which will be compiled with an non-specific hosted
implementation. If your implementation accepts this silently, remember
that your next compiler (or even the next revision of your current one)
is free to vomit at this non-standard return type.
main returns an int in any C program portable across hosted implementations.
Dec 23 '05 #3

P: n/a
sinbad <si***********@gmail.com> wrote:
is it possible to pass variable no.of arguments to a macro ...


As stated, not in C89. It's possible in some cases to kludge your way
around this limitation, however. For example,

#define MY_ASSERT( cond, msg ) if( cond ) {printf msg; abort();}

can be used like

MY_ASSERT( 4 == 4, ("This will never be printed.") );

and

MY_ASSERT( some_var == 4, ("Error, some_var was not 4 (%u)",some_var) );

Solutions of this sort may be useful but are not always stylistically
acceptable. YMMV.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dec 23 '05 #4

P: n/a
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
#define MY_ASSERT( cond, msg ) if( cond ) {printf msg; abort();}


You may notice that I intended

#define MY_ASSERT( cond, msg ) if( !(cond) ) {printf msg; abort();}

Sorry.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dec 23 '05 #5

P: n/a

Christopher Benson-Manica schreef:
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
#define MY_ASSERT( cond, msg ) if( cond ) {printf msg; abort();}


You may notice that I intended

#define MY_ASSERT( cond, msg ) if( !(cond) ) {printf msg; abort();}


Which will yield an error message from your compiler (if it's any good)

Try

#define MY_ASSERT( cond, msg ) \
do { \
if( !(cond) ) \
{ \
fprintf(stderr, "ASSERTION FAILED [%s:%d] %s : %s\n", \
__FILE__, __LINE__, #cond, msg}; abort(); \
} \
} while(0)

Now your assert will work as an ordinary statement, that is, no
superfluous ';' in the compilers input stream *and* it will provide the
file and line position of the failed assertion, plus the condition that
failed,
plus any message you may want to provide.

For the sake of debugging, it is also usefull to call some
debug_whatever() function, so you can set a convenient breakpoint in
your debugger.

Besides, error messages belong on the stderr stream.

Dec 23 '05 #6

P: n/a
kl*****@earthling.net wrote:
Now your assert will work as an ordinary statement, that is, no
superfluous ';' in the compilers input stream *and* it will provide the
file and line position of the failed assertion, plus the condition that
failed,
plus any message you may want to provide.
Thanks for the correction, and no, my compiler is not any good, so I'm
not particularly surprised that it isn't emitting warnings on similar
code.
Besides, error messages belong on the stderr stream.


Aye.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dec 23 '05 #7

P: n/a
Richard Heathfield a écrit :
is it possible to pass variable no.of arguments to a macro ...


Not in standard C as currently implemented. If you are fortunate and unique
enough to have a C99 compiler, use ... in the macro text and __VA_ARGS__ in


Just need a recent distribution of Linux including gcc or a mingw
compiler on Windows (like the one coming with Dev-C++ or better
Code::Blocks).

--
A+

Emmanuel Delahaye
Dec 23 '05 #8

P: n/a
Emmanuel Delahaye said:
Richard Heathfield a écrit :
is it possible to pass variable no.of arguments to a macro ...


Not in standard C as currently implemented. If you are fortunate and
unique enough to have a C99 compiler, use ... in the macro text and
__VA_ARGS__ in


Just need a recent distribution of Linux including gcc or a mingw
compiler on Windows (like the one coming with Dev-C++ or better
Code::Blocks).


....provided you are prepared to lower your diagnostics level, or use the
misleading "c99" switch. I am not prepared to do either. Note that I said
"not in >>>standard<<< C as currently implemented".

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 24 '05 #9

P: n/a

Christopher Benson-Manica schreef:
kl*****@earthling.net wrote:
Now your assert will work as an ordinary statement, that is, no
superfluous ';' in the compilers input stream *and* it will provide the
file and line position of the failed assertion, plus the condition that
failed,
plus any message you may want to provide.


Thanks for the correction, and no, my compiler is not any good, so I'm
not particularly surprised that it isn't emitting warnings on similar
code.


My sincere condoleances. I've been forced to use a crappy compiler on
one occasion, so i know what a pain that can be. No option to switch
compilers?
Besides, error messages belong on the stderr stream.


Aye.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.


Dec 24 '05 #10

P: n/a
kl*****@xs4all.nl wrote:
My sincere condoleances. I've been forced to use a crappy compiler on
one occasion, so i know what a pain that can be. No option to switch
compilers?


No. What makes the situation particularly frustrating is that the
vendor makes a more current version freely available, but there are no
resources to move the codebase to that compiler.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dec 27 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.