macro doubt

S

sinbad

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.
 
R

Richard Heathfield

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.
 
M

Martin Ambuhl

sinbad said:
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.
 
C

Christopher Benson-Manica

sinbad said:
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.
 
C

Christopher Benson-Manica

Christopher Benson-Manica said:
#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.
 
K

kleuske

Christopher Benson-Manica schreef:
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.
 
C

Christopher Benson-Manica

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.
 
E

Emmanuel Delahaye

Richard Heathfield a écrit :
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).
 
R

Richard Heathfield

Emmanuel Delahaye said:
Richard Heathfield a écrit :

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".
 
K

kleuske

Christopher Benson-Manica schreef:
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?
 
C

Christopher Benson-Manica

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top