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 .... 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----- 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. 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.
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 ?
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
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 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.
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 ... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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>...
|
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);
|
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
|
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...
|
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...
|
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...
|
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$) {
}
...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |