473,395 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

macro doubt

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
10 1145
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
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
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
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

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Andrew Dalke | last post by:
Here's a proposed Q&A for the FAQ based on a couple recent threads. Appropriate comments appreciated X.Y: Why doesn't Python have macros like in Lisp or Scheme? Before answering that, a...
4
by: jau | last post by:
Hello mates. I'm part of a big project's developer team. We are writting an application in Java and we are planning to add scripting functionality to it. What we exactly are planning is to give...
44
by: Simon Morgan | last post by:
Hi, Can somebody please help me grok the offsetof() macro? I've found an explanation on http://www.embedded.com/shared/printableArticle.jhtml?articleID=18312031 but I'm afraid it still...
4
by: ApexData | last post by:
Hello 1- What is the AutoExec Macro? Is it the same thing as AutoKeys Macro? 2- I'm looking to Control Keys equally on startup for my entire app. I understand that the AutoKeys Macro is the...
4
by: dis_is_eagle | last post by:
HI.In what circumstances we should use a macro and when we should use a function? If we use macro then will the size of the executable increase?Thanks for help. Eric
7
by: apollo135 | last post by:
Dear all, Could someone please clarify and help with byte ordering macro (big/litte endian conversion)? I found thefollowinf macron on the internet but I have a doubt to use it ... ============...
12
by: Fabrice | last post by:
Hi, Lets say I want to define a generic macro to swap bytes in a integer: #define swapbytes(x) ... I have several implementation of the macros, one is generic C, the other one will be an...
10
by: gouqizi.lvcha | last post by:
Hi Friends, I saw a usage of macro like #define B3 "\xA\xB\xC" I don't understand why B3 is digital 10, can ayone point what the logic behind this usage. Rick
16
by: mdh | last post by:
I have asked a few questions about Macros...and think what I have been missing ...and which all who have replied clearly understand...is that this is a Pre-processor action. Hopefully the above is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.