defining a macro

J

junky_fellow

Hi,

Are there any issues if I define a macro as follows:

#define MY_MACRO(a,b); my_function((a),(b));

and calling this macro as follows,
MY_MACRO(a,b);


Is it a good idea to include ";" as a part of macro ? Can this have
any undesired effect ?


thanks in advance for any help ....
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Hi,

Are there any issues if I define a macro as follows:

#define MY_MACRO(a,b); my_function((a),(b));

and calling this macro as follows,
MY_MACRO(a,b);


Is it a good idea to include ";" as a part of macro ? Can this have
any undesired effect ?

Consider the effect of macro expansion on a code fragment like

if (a)
MY_MACRO(a,b);
else
MY_MACRO(c,d);

What will your extraneous semicolon do to this construct?

HTH
- --
Lew Pitcher

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

iD8DBQFFF8cRagVFX4UWr64RAkwlAKDhwatskoEH0QtF6XQjoATnKudyigCfdkcQ
XrfnkiIHvwwQJpRUfJdWODw=
=kJUr
-----END PGP SIGNATURE-----
 
B

Bart

Hi,

Are there any issues if I define a macro as follows:

#define MY_MACRO(a,b); my_function((a),(b));

and calling this macro as follows,
MY_MACRO(a,b);


Is it a good idea to include ";" as a part of macro ? Can this have
any undesired effect ?

Besides being very misleading I don't know. Have you tried:

if(something)
MY_MACRO(a, b);

Regards,
Bart.
 
M

Michael Mair

Are there any issues if I define a macro as follows:

#define MY_MACRO(a,b); my_function((a),(b));

This is wrongly grouped:
#define MY_MACRO(a,b) ;my_function((a),(b));
is equivalent.
and calling this macro as follows,
MY_MACRO(a,b);

Is it a good idea to include ";" as a part of macro ? Can this have
any undesired effect ?

if (foo)
MY_MACRO(a,b);
will compile, link, and seemingly "ignore foo".

See also
http://c-faq.com/cpp/multistmt.html


Cheers
Michael
 
J

junky_fellow

Michael said:
This is wrongly grouped:
#define MY_MACRO(a,b) ;my_function((a),(b));
is equivalent.


if (foo)
MY_MACRO(a,b);
will compile, link, and seemingly "ignore foo".

I tried that and it seemed to work fine. Please note that ";" is a
part of macro
and during preprocessing MY_MACRO(a,b); will be replaced by
my_function(a,b);
So, there should not be any problem.

Am I missing something ?
 
T

Tak-Shing Chan

I tried that and it seemed to work fine. Please note that ";" is a
part of macro
and during preprocessing MY_MACRO(a,b); will be replaced by
my_function(a,b);
So, there should not be any problem.

Am I missing something ?

Yes. The code above expands into:

if (foo)
;my_function((a),(b));;

Which is semantically equivalent to:

if (foo)
; /* do nothing, effectively ignoring foo */
my_function((a), (b));
; /* do nothing again */

Tak-Shing
 
R

Richard Tobin

[/QUOTE]
I tried that and it seemed to work fine. Please note that ";" is a
part of macro and during preprocessing MY_MACRO(a,b); will be replaced by
my_function(a,b);

It doesn't work like that. You can't put stuff after the close
bracket and expect corresponding stuff in the program to be replaced.

-- Richard
 
S

Snis Pilbor

Hi,

Are there any issues if I define a macro as follows:

#define MY_MACRO(a,b); my_function((a),(b));

and calling this macro as follows,
MY_MACRO(a,b);


Is it a good idea to include ";" as a part of macro ? Can this have
any undesired effect ?


thanks in advance for any help ....

What I like to do is this:

#define MY_MACRO(a,b) do \
{ \
my_function((a),(b)); \
} \
while( 0 )

This way, MY_MACRO inherits the same semantics as if it were a
function: if you "call" it without manually adding a semicolon, the
compiler will report an error because the ending while of a do-loop
must be terminated with a semicolon. And, more importantly, it works
fine regardless of how you combine it with if's, else's, etc.

This is an FAQ btw =) You should set a goal of reading through the
comp.lang.c FAQs, not all in one sitting but a bit at a time. Once you
finish, you will gain 2,345,098,721 experience points ;)

Snis P.
 
J

junky_fellow

Tak-Shing Chan said:
Yes. The code above expands into:

if (foo)
;my_function((a),(b));;

Which is semantically equivalent to:

if (foo)
; /* do nothing, effectively ignoring foo */
my_function((a), (b));
; /* do nothing again */


Now I got this. Thanks everyone ...
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top