473,386 Members | 1,720 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,386 software developers and data experts.

defining a macro

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

Sep 25 '06 #1
8 2169
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
ju**********@yahoo.co.in wrote:
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

iD8DBQFFF8cRagVFX4UWr64RAkwlAKDhwatskoEH0QtF6XQjoA TnKudyigCfdkcQ
XrfnkiIHvwwQJpRUfJdWODw=
=kJUr
-----END PGP SIGNATURE-----

Sep 25 '06 #2
ju**********@yahoo.co.in wrote:
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.

Sep 25 '06 #3
ju**********@yahoo.co.in schrieb:
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 25 '06 #4

Michael Mair wrote:
ju**********@yahoo.co.in schrieb:
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".
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 ?

Sep 25 '06 #5
On Mon, 25 Sep 2006, ju**********@yahoo.co.in wrote:
>
Michael Mair wrote:
>ju**********@yahoo.co.in schrieb:
>> 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".

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
Sep 25 '06 #6
In article <11**********************@m73g2000cwd.googlegroups .com>,
ju**********@yahoo.co.in <ju**********@yahoo.co.inwrote:
#define MY_MACRO(a,b); my_function((a),(b));
>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
Sep 25 '06 #7

ju**********@yahoo.co.in wrote:
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.

Sep 25 '06 #8

Tak-Shing Chan wrote:
On Mon, 25 Sep 2006, ju**********@yahoo.co.in wrote:

Michael Mair wrote:
ju**********@yahoo.co.in schrieb:
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".
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 */

Now I got this. Thanks everyone ...

Sep 25 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Matt Garman | last post by:
I'd like to create a "custom output facility". In other words, I want an object whose use is similar to std::cout/std::cerr, but offers more flexibility. Instead of simply writing the parameter...
7
by: Dave Ohlsson | last post by:
Hi, In ISO C/C++, a string constant prefixed by the letter `L' is a wide string constant and is of type "array of wchar_t". Consider the following C program fragment: #include <stdio.h>...
2
by: jut_bit_zx | last post by:
Consider the following code: #include <iostream> #include <map> using namespace std; typedef struct tagPOINT { tagPOINT(); bool operator == (const tagPOINT& point);
8
by: hobbes_7_8 | last post by:
Hi everybody! This is basically a pre-processor doubt. I have this very simple define: #ifdef NDEBUG #define QTRACE // #else #define QTRACE qDebug() #endif
4
by: Max TenEyck Woodbury | last post by:
I need a macro that will take an arbitrarily long list of arguments where each argument needs to be passed to another macro. Is it possible to write such a macro? If so, could you please...
7
by: Paul | last post by:
I would like to define a list of parameters, and then pass them to a macro. However, the compiler (gcc) only sees one parameter, rather than expanding the definition. Could anyone suggest a way of...
5
by: =?GB2312?B?17/HvyBaaHVvLCBRaWFuZw==?= | last post by:
Hi, I would like to have someone comments on what's the best practice defining error codes in C. Here's what I think: solution A: using enum pros: type safe. better for debug (some debugger...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.